this is the logo image

浅谈go短变量声明

Go的短变量声明(short variable declaration)用于变量的声明和初始化,一般用于本地代码块中(local code block)中。语法如下:
     variable := value/expression/function
变量的类型由对应的值(value)/表达式(expression)/函数(function)推断而来。举例如下:

                    package main
                
                    import "fmt"
                
                    func main() {
                        // Short varible declarations
                        a := 5          // type int, value 5
                        b := 2.3 + 5.6  // type float64, value 7.9
                        c := getbool()  // type bool, value true
                        
                        // Print all the values of the varibales 
                        fmt.println(a, b, c)
                    }
                
                    // Func to return true
                    func getbool() bool {
                        return true
                    }
                    

在命令行运行命令:go run main.go(文件名), 结果如下:

        5 7.9 true