本文介绍了function()用作值编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过调整示例来学习Go的基础知识,方法如下:

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))
}

我遇到此异常:

有任何明显的错误吗?

推荐答案

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

package main

import (
        "fmt"
        "strings"
)

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

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

游乐场

输出:

SERGIO

这篇关于function()用作值编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:48