On 18/09/17 10:00, Ângelo Chida wrote:
Hi, I'm new in Golang but I'm PHP StackDev since 10 years ago
I'm following this tutorial https://golang.org/doc/articles/wiki/ on
how to build a web app, but I'm stuck on some error and can't find
anywhere a solution.
If you read down into "Data Structure" section, you will find the
explanation for those function.
> Functions can return multiple values. The standard library function
|io.ReadFile|returns |[]byte|and |error|. In |loadPage|, error isn't
being handled yet; the "blank identifier" represented by the underscore
(|_|) symbol is used to throw away the error return value (in essence,
assigning the value to nothing).
>
> But what happens if |ReadFile|encounters an error? For example, the
file might not exist. We should not ignore such errors. Let's modify the
function to return |*Page|and |error|.
>
> func loadPage(title string) (*Page, error) {
> filename := title + ".txt"
> body, err := ioutil.ReadFile(filename)
> if err != nil {
> return nil, err
> }
> return &Page{Title: title, Body: body}, nil
> }
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.