cannot assign 1 values to 2 variables [simple error handling]

5,733 views
Skip to first unread message

Ângelo Chida

unread,
Sep 18, 2017, 1:06:42 AM9/18/17
to golang-nuts
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. Please help
Take a look at the image
My go version is 1.9

*sorry for poor english, it is not my native language




Ian Lance Taylor

unread,
Sep 18, 2017, 1:25:39 AM9/18/17
to Ângelo Chida, golang-nuts
You are trying to assign the result of loadPage to two variables, but loadPage only returns one result.

In the "here is OK" section you are assigning the result of ioutil.ReadFile to two variables.  That is OK because ioutil.ReadFile returns two results.  You can see the definition of ioutil.ReadFile at https://golang.org/src/io/ioutil/ioutil.go#L45 .

Ian



--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Elias Naur

unread,
Sep 18, 2017, 6:35:56 AM9/18/17
to golang-nuts
Just to expand on Ian's reply: you seem to want to have loadPage return any errors, similar to what ReaedFile does. If so, you need to declare loadPage to return two values, like so:

func loadPage(title string) (*Page, error)

And return a nil *Page and the error if ReadFile fails:

body, err := ioutil.ReadFile(filename)
if err != nil {
    return nil, err
}

The same goes for the sucess path:

return &Page{Title: title, Body: body}, nil

HTH,
 - elias
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Mhd Shulhan

unread,
Sep 18, 2017, 7:41:27 AM9/18/17
to Ângelo Chida, golang-nuts
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
>    }
Message has been deleted

Ângelo Chida

unread,
Sep 18, 2017, 10:13:48 AM9/18/17
to golang-nuts
I understand now, thank you all
Reply all
Reply to author
Forward
0 new messages