gpt4 book ai didi

go - function() used as value 编译错误

转载 作者:IT王子 更新时间:2023-10-29 01:51:40 25 4
gpt4 key购买 nike

我正在尝试通过调整示例来学习 Go 的基础知识,因为我正在学习位于此处的教程:

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


这是我编写的一个小函数,它可以将所有字符转换为全部大写。

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

有什么明显的错误吗?

最佳答案

您缺少 capitalize() 的返回类型:

package main

import (
"fmt"
"strings"
)

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

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

Playground


输出:

SERGIO

关于go - function() used as value 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16596805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com