Returning a string variable

950 views
Skip to first unread message

Adam Jimerson

unread,
Apr 6, 2010, 12:10:53 AM4/6/10
to golan...@googlegroups.com
Can someone here help me out, I know that it is possible to return a variable in Go but when I try using the following:

backupDir, err := ioutil.ReadFile(Home + "/.backup-lightrc")
if err != nil {
fmt.Printf("Error: Can't read config file, %s", err.String())
os.Exit(1)
}
Config.Close()
return backupDir
}

I get this from 6g:

backup-light.go:149: too many arguments to return

How is that too many arguments to return?

jimmy frasche

unread,
Apr 6, 2010, 12:13:13 AM4/6/10
to Adam Jimerson, golan...@googlegroups.com
Did you define a return type for the function?

Ian Lance Taylor

unread,
Apr 6, 2010, 12:16:29 AM4/6/10
to Adam Jimerson, golan...@googlegroups.com
Adam Jimerson <ven...@gmail.com> writes:

That is just a fragment of the code. What does the func line look
like? That is the error message I would expect to see if your
function does not have a return type.

Ian

Adam Jimerson

unread,
Apr 6, 2010, 9:58:02 AM4/6/10
to golan...@googlegroups.com
Sorry for the fragment of code I was trying to not post more than needed, here is the whole function:

func ReadConfig() ([]uint8) {
Home := os.Getenv("HOME")
Config, err := os.Open(Home + "/.backup-light/backup-lightrc", os.O_RDONLY, 755)
if Config == nil {
fmt.Printf("Error: Can't open file, %s\n", err.String())
fmt.Println("\nCreate a config file (y/n): ")
input := bufio.NewReader(os.Stdin)
result, _ := input.ReadString('\n')
text := strings.TrimSpace(result)
switch {
case text == "y" || text == "Y":
CreateConfig()
case text == "n" || text == "N":
os.Exit(0)
default:
fmt.Println("Error: that was not yes or no!\n")
os.Exit(1)
}
} else {
//backupDir, err := Config.Read() //Using ioutil.ReadFile for now
//Need to figure out what is meant by len(b) in the doc.
backupDir, err := ioutil.ReadFile(Home + "/.backup-lightrc")
if err != nil {
fmt.Printf("Error: Can't read config file, %s", err.String())
os.Exit(1)
}
Config.Close()
return backupDir
}
}

Because I am using := to declare the variable that I am wanting to return I assume it is []uint8, if I am wrong about this could that be why it is throwing me that error?

chris dollin

unread,
Apr 6, 2010, 10:17:21 AM4/6/10
to Adam Jimerson, golan...@googlegroups.com
On 6 April 2010 14:58, Adam Jimerson <ven...@gmail.com> wrote:

The code you show in that message won't provoke the error
you reported in your original post. It provokes a different error
(and various other irrelevant-to-your-problem errors).

When you get a problem like this, try and cut your code down to
the smallest example that shows the problem. Sometimes that's
enough -- you find out yourself what's wrong.


func ReadConfig() ([]uint8) {

...
 
backupDir, err := ioutil.ReadFile(Home + "/.backup-lightrc")
if err != nil {
fmt.Printf("Error: Can't read config file, %s", err.String())
os.Exit(1)
}
Config.Close()
return backupDir

Because I am using := to declare the variable that I am wanting to return
I assume it is []uint8, if I am wrong about this could that be why it is throwing me that error?

I think you're confused about "return variables".

You can declare a variable /in the return-type/ of the function:

  func ForExample( i int ) (result string) { ... return ... }

`result` is a return variable of type string. A return with no
operands will return the values of the return variables,
in this case whatever happens to be the value of `result`.

A variable declared using := /cannot/ be a return variable.
But you can have operands to return

  return "hello"
  return "hello" + ", world"
  return greeting("EN-UK")
  return 0, false
  return answer

"answer" isn't a Go return variable, just a variable that happens
to be used in the return statement as an operand.
 
The type of a variable declared with `v := e` is the type of the
expression e, not whatever type would make sense for the
places it's used later/

--
Chris "allusive" Dollin

Steven

unread,
Apr 6, 2010, 11:11:33 AM4/6/10
to chris dollin, Adam Jimerson, golan...@googlegroups.com

A string variable has type string, not type []uint8.

chris dollin

unread,
Apr 6, 2010, 11:16:31 AM4/6/10
to Steven, Adam Jimerson, golan...@googlegroups.com
On 6 April 2010 16:11, Steven <stev...@gmail.com> wrote:

A string variable has type string, not type []uint8.

Curses! I was thinking so hard about how to return the result I
didn't look hard enough at the types. Well caught.

Chris

--
Chris "time to go home, tra la la, time to go home" Dollin

Adam Jimerson

unread,
Apr 6, 2010, 11:22:18 AM4/6/10
to golan...@googlegroups.com
Sorry didn't send it to the list

On Tue, Apr 6, 2010 at 4:10 PM, chris dollin <ehog....@googlemail.com> wrote:

Yes, they can. := is a shorthand declaration; it doesn't stop the
things it declares being explicit operands of a return statement.

Then what was meant when you said "A variable declared using := /cannot/ be a return variable.", this is confusing me the most

 If I did have a statement like 'v := e' then I cannot return 'v'?  That is what I was originally doing, even before I tried adding []uint8 to the return type of the func line, and if I tried to say it was a string in the return type it would throw up a error basically saying string != []uint8.


Daniel Smith

unread,
Apr 6, 2010, 11:36:43 AM4/6/10
to Adam Jimerson, golan...@googlegroups.com
In Chris's example,

func ForExample( i int ) (result string) { ... return ... }


"result" is what he is calling a return variable (I'd prefer "return parameter" or at least "named return variable" to avoid confusion with a regular variable whose contents you happen to be returning). You could not in the body of the function redeclare "result" by using the := operator. There is no restriction on returning variables which were declared with :=. It sounds like your code wasn't working initially because you were missing the return type (the only way to get "too many returns" when returning a single value). At the moment, it doesn't appear to return a value for every control path.

chris dollin

unread,
Apr 6, 2010, 12:32:27 PM4/6/10
to Daniel Smith, Adam Jimerson, golan...@googlegroups.com
On 6 April 2010 16:36, Daniel Smith <luken...@gmail.com> wrote:
In Chris's example,

func ForExample( i int ) (result string) { ... return ... }


"result" is what he is calling a return variable (I'd prefer "return parameter" or at least "named return variable" to avoid confusion with a regular variable whose contents you happen to be returning).

Yes -- I wrote originally thinking that was the term in the Go
documentation, but it must have come from somewhere else.

I did try and explain what I meant, but it looks like the damage had
already been done.

"return parameter"? "return argument"? "result parameter"?

--
Chris "i don't even /like/ them!" Dollin
Reply all
Reply to author
Forward
0 new messages