Error question: "cannot use nil as type string in return argument"

7,695 views
Skip to first unread message

Travis Reeder

unread,
Jun 13, 2011, 5:57:48 PM6/13/11
to golan...@googlegroups.com
Forgive my noobness, but why can't I return nil for string return?

I get "cannot use nil as type string in return argument" with the following:

func nextLine(src *bufio.Reader) (string, os.Error) {
line, isPrefix, err := src.ReadLine()
// discard the rest of long lines
for isPrefix {
_, isPrefix, err = src.ReadLine()
}
if line == nil {
return nil, nil
}
if err != nil {
return nil, err
}
line_s := string(line)
return line_s, nil
}


andrey mirtchovski

unread,
Jun 13, 2011, 6:00:42 PM6/13/11
to golan...@googlegroups.com
use "" for string nils.

Jan Mercl

unread,
Jun 14, 2011, 4:47:54 AM6/14/11
to golan...@googlegroups.com
On Monday, June 13, 2011 11:57:48 PM UTC+2, Travis Reeder wrote:
Forgive my noobness, but why can't I return nil for string return?


A value x is assignable to a variable of type T ("x is assignable to T") in any of these cases:
...
x is the predeclared identifier nil and T is a pointer, function, slice, map, channel, or interface type.
... 

The specs clause doesn't mention string, so "stringvar = nil" is (ATM) out of specs.
Reply all
Reply to author
Forward
0 new messages