golang 基础(9)函数

作者: zidea | 来源:发表于2019-03-17 19:18 被阅读4次
    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

    相关文章

      网友评论

        本文标题:golang 基础(9)函数

        本文链接:https://www.haomeiwen.com/subject/xmtpmqtx.html