英文:
function() used as value compile error
问题
我正在尝试通过调整示例来学习Go的基础知识,教程位于这里:
这是我写的一个小函数,它只是将每个字符都转换为大写。
package main
import (
"fmt"
"strings"
)
func capitalize(name string) {
name = strings.ToTitle(name)
return
}
func main() {
test := "Sergio"
fmt.Println(capitalize(test))
}
我得到了这个异常:
>prog.go:15: capitalize(test) used as value
有什么明显的错误吗?
英文:
I'm trying to learn the basics of Go by tweaking examples as I go along the tutorial located here:
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?
答案1
得分: 12
你忘记了为capitalize()
函数指定返回类型:
package main
import (
"fmt"
"strings"
)
func capitalize(name string) string {
return strings.ToTitle(name)
}
func main() {
test := "Sergio"
fmt.Println(capitalize(test))
}
输出:
SERGIO
英文:
You are missing the return type for capitalize()
:
package main
import (
"fmt"
"strings"
)
func capitalize(name string) string {
return strings.ToTitle(name)
}
func main() {
test := "Sergio"
fmt.Println(capitalize(test))
}
Output:
SERGIO
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论