程序员深入浅出golangGolang 入门资料+笔记

golang 基础(9)函数

2019-03-17  本文已影响4人  zidea
square-gopher.png

函数

函数是由函数名,参数,返回值和函数体所组成。

func add(a, b int) int {} 

定义函数并且复习一下之前的 switch 语句

func eval(a, b int,op string) int {
    switch op {
    case "+":
        return a + b
    case "-":
        return a - b
    case "*":
        return a * b
    case "/":
        return a / b
    default:
        panic("unsupported operation: " + op)
    }
}

在 go 语言中 { 需要 bar() 在同一行不言就会报错

func bar() //报错
{

}
.\funtions.go:26:6: missing function body
.\funtions.go:27:1: syntax error: unexpected semicolon or newline before {
images.png
上一篇 下一篇

猜你喜欢

热点阅读