du1913 2013-05-29 19:18
浏览 121
已采纳

在Go中设置函数参数类型

I'm a complete beginner to the Go programming language, and I'm trying to define the parameter types of a Go function called addStuff that simply adds two integers and returns their sum, but I see the following error when I try to compile the function:

prog.go:6: undefined: a
prog.go:6: undefined: b
prog.go:7: undefined: a
prog.go:7: undefined: b
prog.go:7: too many arguments to return
prog.go:11: addStuff(4, 5) used as value

Here's the code that produced this compiler error:

package main

import "fmt"
import "strconv"

func addStuff(a, b){
    return a+b
}

func main() {
    fmt.Println("Hello," + strconv.Itoa(addStuff(4,5)))
}

What am I doing wrong here, and what is the correct way to set the types of parameters in Go?

  • 写回答

1条回答 默认 最新

  • douxian4888 2013-05-29 19:21
    关注
    func addStuff(a int, b int) int {
        return a+b
    }
    

    This will make a and b parameters of type int, and have the function return an int. An alternative is func addStuff(a, b int) int which will also make both a and b parameters of type int.

    I highly recommend A Tour of Go which teaches the basics of Go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示