I'm trying to learn the basics of Go by tweaking examples as I go along the tutorial located here:

http://tour.golang.org/#9


Here's a small function I wrote that just turns ever character to all caps.

package main

import (
    "fmt"
    "strings"
)

func capitalize(name string) {
    name = strings.ToTitle(name)
    return
}

func main() {
    test := "Sergio"    
    fmt.Println(capitalize(test))
}

I'm getting this exception:

prog.go:15: capitalize(test) used as value

Any glaring mistakes?

up vote 5 down vote accepted
package main

import (
        "fmt"
        "strings"
)

func capitalize(name string) string {
        return strings.ToTitle(name)
}

func main() {
        test := "Sergio"
        fmt.Println(capitalize(test))
}

Playground


Output:

SERGIO
  • Gah, I forgot the return type for the function didn't I? – sergserg May 16 '13 at 20:30
  • yepp. And you did assign to the argument instead of to the return value. – fuz May 16 '13 at 22:12
  • @FUZxxl: There's nothing wrong with that. In Go, arguments are passed by value. For example, func capitalize(name string) string { name = strings.ToTitle(name); return name }. – peterSO May 16 '13 at 23:49
  • @peterSO I know that. I wanted to point out that this did not do what OP wanted. – fuz May 17 '13 at 11:46

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.