// 声明包名,可以是任意名字,但只有main才可以编译成二进制文件.exe packge main // 导入 fmt 标准包 import "fmt" // 函数名 main 可以是任意的名字,但go文件的入口文件一般就定义为:main func main() { fmt.Println("Hello world") // 控制台打印: "Hello world" }
# 编译 helloworld.go 文件,生成 'helloworld.exe' 可执行的二进制文件 go build helloworld.go
# go 程序的详细编译过程,需要加一个 '-x' 参数 go build -x helloworld.go G:\Golang\day01>go build -x helloworld.go WORK=C:\Users\ADMINI~1\AppData\Local\Temp\go-build058986106 mkdir -p $WORK\b001\ cat >$WORK\b001\importcfg.link << 'EOF' # internal packagefile command-line-arguments=C:\Users\Administrator\AppData\Local\go-build\b8\b82385b45455bcbdf17588943c44929778c0664c2bcdcce49612bd35efd7bd22-d packagefile fmt=C:\Go\pkg\windows_amd64\fmt.a packagefile runtime=C:\Go\pkg\windows_amd64\runtime.a packagefile errors=C:\Go\pkg\windows_amd64\errors.a packagefile internal/fmtsort=C:\Go\pkg\windows_amd64\internal\fmtsort.a packagefile io=C:\Go\pkg\windows_amd64\io.a packagefile math=C:\Go\pkg\windows_amd64\math.a packagefile os=C:\Go\pkg\windows_amd64\os.a packagefile reflect=C:\Go\pkg\windows_amd64\reflect.a packagefile strconv=C:\Go\pkg\windows_amd64\strconv.a packagefile sync=C:\Go\pkg\windows_amd64\sync.a packagefile unicode/utf8=C:\Go\pkg\windows_amd64\unicode\utf8.a packagefile internal/bytealg=C:\Go\pkg\windows_amd64\internal\bytealg.a packagefile internal/cpu=C:\Go\pkg\windows_amd64\internal\cpu.a packagefile runtime/internal/atomic=C:\Go\pkg\windows_amd64\runtime\internal\atomic.a packagefile runtime/internal/math=C:\Go\pkg\windows_amd64\runtime\internal\math.a packagefile runtime/internal/sys=C:\Go\pkg\windows_amd64\runtime\internal\sys.a packagefile internal/reflectlite=C:\Go\pkg\windows_amd64\internal\reflectlite.a packagefile sort=C:\Go\pkg\windows_amd64\sort.a packagefile sync/atomic=C:\Go\pkg\windows_amd64\sync\atomic.a packagefile math/bits=C:\Go\pkg\windows_amd64\math\bits.a packagefile internal/oserror=C:\Go\pkg\windows_amd64\internal\oserror.a packagefile internal/poll=C:\Go\pkg\windows_amd64\internal\poll.a packagefile internal/syscall/execenv=C:\Go\pkg\windows_amd64\internal\syscall\execenv.a packagefile internal/syscall/windows=C:\Go\pkg\windows_amd64\internal\syscall\windows.a packagefile internal/testlog=C:\Go\pkg\windows_amd64\internal\testlog.a packagefile syscall=C:\Go\pkg\windows_amd64\syscall.a packagefile time=C:\Go\pkg\windows_amd64\time.a packagefile unicode/utf16=C:\Go\pkg\windows_amd64\unicode\utf16.a packagefile unicode=C:\Go\pkg\windows_amd64\unicode.a packagefile internal/race=C:\Go\pkg\windows_amd64\internal\race.a packagefile internal/syscall/windows/sysdll=C:\Go\pkg\windows_amd64\internal\syscall\windows\sysdll.a packagefile internal/syscall/windows/registry=C:\Go\pkg\windows_amd64\internal\syscall\windows\registry.a EOF mkdir -p $WORK\b001\exe\ cd . "C:\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\go-build058986106\\b001\\exe\\a.out.exe" -importcfg "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\go-build058986106\\b001\\importcfg.link" -buildmode=exe -buildid=Va9osQyTjZlRtuDFNn2t/jQDKertcdlkDzi7CCK_8/0q33Isr5dtp6VBFPeoXK/Va9osQyTjZlRtuDFNn2t -extld=gcc "C:\\Users\\Administrator\\AppData\\Local\\go-build\\b8\\b82385b45455bcbdf17588943c44929778c0664c2bcdcce49612bd35efd7bd22-d" "C:\\Go\\pkg\\tool\\windows_amd64\\buildid.exe" -w "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\go-build058986106\\b001\\exe\\a.out.exe" # internal cp $WORK\b001\exe\a.out.exe helloworld.exe rm -r $WORK\b001\
# 以上默认编译出的exe文件为: helloworld.exe,若需要更改编译后的二进制文件名字为: test.exe,则加一个 '-o' 参数 go build -x -o test.exe helloworld.go
# go 程序在编译时会生成一个临时目录,如果要看生成的临时目录,则需要加一个 '-work' 参数 go build -x -o test.exe -work helloworld.go # WORK=C:\Users\ADMINI~1\AppData\Local\Temp\go-build037181318 G:\Golang\day01>go build -x -o test.exe -work helloworld.go WORK=C:\Users\ADMINI~1\AppData\Local\Temp\go-build037181318 mkdir -p $WORK\b001\ cat >$WORK\b001\importcfg.link << 'EOF' # internal packagefile command-line-arguments=C:\Users\Administrator\AppData\Local\go-build\b8\b82385b45455bcbdf17588943c44929778c0664c2bcdcce49612bd35efd7bd22-d packagefile fmt=C:\Go\pkg\windows_amd64\fmt.a packagefile runtime=C:\Go\pkg\windows_amd64\runtime.a packagefile errors=C:\Go\pkg\windows_amd64\errors.a packagefile internal/fmtsort=C:\Go\pkg\windows_amd64\internal\fmtsort.a packagefile io=C:\Go\pkg\windows_amd64\io.a packagefile math=C:\Go\pkg\windows_amd64\math.a packagefile os=C:\Go\pkg\windows_amd64\os.a packagefile reflect=C:\Go\pkg\windows_amd64\reflect.a packagefile strconv=C:\Go\pkg\windows_amd64\strconv.a packagefile sync=C:\Go\pkg\windows_amd64\sync.a packagefile unicode/utf8=C:\Go\pkg\windows_amd64\unicode\utf8.a packagefile internal/bytealg=C:\Go\pkg\windows_amd64\internal\bytealg.a packagefile internal/cpu=C:\Go\pkg\windows_amd64\internal\cpu.a packagefile runtime/internal/atomic=C:\Go\pkg\windows_amd64\runtime\internal\atomic.a packagefile runtime/internal/math=C:\Go\pkg\windows_amd64\runtime\internal\math.a packagefile runtime/internal/sys=C:\Go\pkg\windows_amd64\runtime\internal\sys.a packagefile internal/reflectlite=C:\Go\pkg\windows_amd64\internal\reflectlite.a packagefile sort=C:\Go\pkg\windows_amd64\sort.a packagefile sync/atomic=C:\Go\pkg\windows_amd64\sync\atomic.a packagefile math/bits=C:\Go\pkg\windows_amd64\math\bits.a packagefile internal/oserror=C:\Go\pkg\windows_amd64\internal\oserror.a packagefile internal/poll=C:\Go\pkg\windows_amd64\internal\poll.a packagefile internal/syscall/execenv=C:\Go\pkg\windows_amd64\internal\syscall\execenv.a packagefile internal/syscall/windows=C:\Go\pkg\windows_amd64\internal\syscall\windows.a packagefile internal/testlog=C:\Go\pkg\windows_amd64\internal\testlog.a packagefile syscall=C:\Go\pkg\windows_amd64\syscall.a packagefile time=C:\Go\pkg\windows_amd64\time.a packagefile unicode/utf16=C:\Go\pkg\windows_amd64\unicode\utf16.a packagefile unicode=C:\Go\pkg\windows_amd64\unicode.a packagefile internal/race=C:\Go\pkg\windows_amd64\internal\race.a packagefile internal/syscall/windows/sysdll=C:\Go\pkg\windows_amd64\internal\syscall\windows\sysdll.a packagefile internal/syscall/windows/registry=C:\Go\pkg\windows_amd64\internal\syscall\windows\registry.a EOF mkdir -p $WORK\b001\exe\ cd . "C:\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\go-build037181318\\b001\\exe\\a.out.exe" -importcfg "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\go-build037181318\\b001\\importcfg.link" -buildmode=exe -buildid=Va9osQyTjZlRtuDFNn2t/jQDKertcdlkDzi7CCK_8/0q33Isr5dtp6VBFPeoXK/Va9osQyTjZlRtuDFNn2t -extld=gcc "C:\\Users\\Administrator\\AppData\\Local\\go-build\\b8\\b82385b45455bcbdf17588943c44929778c0664c2bcdcce49612bd35efd7bd22-d" "C:\\Go\\pkg\\tool\\windows_amd64\\buildid.exe" -w "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\go-build037181318\\b001\\exe\\a.out.exe" # internal cp $WORK\b001\exe\a.out.exe test.exe
# 'go run' 可以直接先进行编译后再运行 go run helloworld.go G:\Golang\day01>go run helloworld.go Hello world # 加参数 '-x' 会打印出完整的过程(现编以后运行) go run -x helloworld.go # 加参数 '-n' 只会打印出编译的过程,不会运行 go run -n helloworld.go G:\Golang\day01>go run -n helloworld.go mkdir -p $WORK\b001\ cat >$WORK\b001\importcfg.link << 'EOF' # internal packagefile command-line-arguments=C:\Users\Administrator\AppData\Local\go-build\13\1393673cb1fc6a992fd0d9e1e30f237e0b61dfd69337713c72ba8ff94fef9496-d packagefile fmt=C:\Go\pkg\windows_amd64\fmt.a packagefile runtime=C:\Go\pkg\windows_amd64\runtime.a packagefile errors=C:\Go\pkg\windows_amd64\errors.a packagefile internal/fmtsort=C:\Go\pkg\windows_amd64\internal\fmtsort.a packagefile io=C:\Go\pkg\windows_amd64\io.a packagefile math=C:\Go\pkg\windows_amd64\math.a packagefile os=C:\Go\pkg\windows_amd64\os.a packagefile reflect=C:\Go\pkg\windows_amd64\reflect.a packagefile strconv=C:\Go\pkg\windows_amd64\strconv.a packagefile sync=C:\Go\pkg\windows_amd64\sync.a packagefile unicode/utf8=C:\Go\pkg\windows_amd64\unicode\utf8.a packagefile internal/bytealg=C:\Go\pkg\windows_amd64\internal\bytealg.a packagefile internal/cpu=C:\Go\pkg\windows_amd64\internal\cpu.a packagefile runtime/internal/atomic=C:\Go\pkg\windows_amd64\runtime\internal\atomic.a packagefile runtime/internal/math=C:\Go\pkg\windows_amd64\runtime\internal\math.a packagefile runtime/internal/sys=C:\Go\pkg\windows_amd64\runtime\internal\sys.a packagefile internal/reflectlite=C:\Go\pkg\windows_amd64\internal\reflectlite.a packagefile sort=C:\Go\pkg\windows_amd64\sort.a packagefile sync/atomic=C:\Go\pkg\windows_amd64\sync\atomic.a packagefile math/bits=C:\Go\pkg\windows_amd64\math\bits.a packagefile internal/oserror=C:\Go\pkg\windows_amd64\internal\oserror.a packagefile internal/poll=C:\Go\pkg\windows_amd64\internal\poll.a packagefile internal/syscall/execenv=C:\Go\pkg\windows_amd64\internal\syscall\execenv.a packagefile internal/syscall/windows=C:\Go\pkg\windows_amd64\internal\syscall\windows.a packagefile internal/testlog=C:\Go\pkg\windows_amd64\internal\testlog.a packagefile syscall=C:\Go\pkg\windows_amd64\syscall.a packagefile time=C:\Go\pkg\windows_amd64\time.a packagefile unicode/utf16=C:\Go\pkg\windows_amd64\unicode\utf16.a packagefile unicode=C:\Go\pkg\windows_amd64\unicode.a packagefile internal/race=C:\Go\pkg\windows_amd64\internal\race.a packagefile internal/syscall/windows/sysdll=C:\Go\pkg\windows_amd64\internal\syscall\windows\sysdll.a packagefile internal/syscall/windows/registry=C:\Go\pkg\windows_amd64\internal\syscall\windows\registry.a EOF mkdir -p $WORK\b001\exe\ cd . "C:\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "$WORK\\b001\\exe\\helloworld.exe" -importcfg "$WORK\\b001\\importcfg.link" -s -w -buildmode=exe -buildid=O97mupZ1w3kiblxmWDWH/fYUzxKmhHIgbtJYCRI93/451t1_THFAFC5bnaPOQF/O97mupZ1w3kiblxmWDWH -extld=gcc "C:\\Users\\Administrator\\AppData\\Local\\go-build\\13\\1393673cb1fc6a992fd0d9e1e30f237e0b61dfd69337713c72ba8ff94fef9496-d" $WORK\b001\exe\helloworld.exe
# vs code IDE 会在保存go 程序是自动格式化代码,go 也提供了 'go fmt' 命令来格式代码 go fmt helloworld.go
package main import "fmt" func main() { // var 变量名 类型 = 变量值 var msg string = "hello world !" fmt.Println(msg) }
package main import ( "fmt" ) func main() { var initVarValue string = "初始化变量与变量值" // 仅初始化变量的类型,未定义变量的值 // 相当于 python 中的:not_innit_var_value = "" var notInitVarValue string // 仅初始化值,但不定义变量的类型,让其根据变量的值(此时变量的值便不能省略)来推导变量的类型 var notInitVarType = "未定义变量的类型,仅定义了变量的值来推导变量的类型" // 短声明(注意:仅可以在包级别使用!) shortVar := "不用关键字`var`,用`:`代替变量的类型" fmt.Println(initVarValue) fmt.Println(notInitVarValue) fmt.Println(notInitVarType) fmt.Println(shortVar) } /* 初始化变量与变量值 未定义变量的类型,仅定义了变量的值来推导变量的类型 不用关键字`var`,用`:`代替变量的类型 */
package main import "fmt" func main() { /* var a string = "最全定义变量的方式" var b = "省略变量的类型" // 省略变量的初始值(此类型的零值) var c string */ // 此三个相同类型的变量则可以合并定义 var ( a string = "最全定义变量的方式" b = "省略变量的类型" c string ) fmt.Println(a) fmt.Println(b) fmt.Println(c) /* d := "短声明d" e := "短声明e" */ d, e := "短声明d", "短声明e" fmt.Println(d) fmt.Println(e) } /* 最全定义变量的方式 省略变量的类型 短声明d 短声明e */
package main import ( "fmt" ) // 包级别的变量 var packageVar = "包级别的变量" func main() { // 函数级别的变量 var funcVar string = "函数级别的变量" fmt.Println(packageVar, funcVar) { // 块级别的变量 var blockVar = "块级别的变量" fmt.Println(packageVar, funcVar, blockVar) { // 子块级别的变量 var innerBlockVar = "子块级别的变量" fmt.Println(packageVar, funcVar, blockVar, innerBlockVar) } } } /* 包级别的变量 函数级别的变量 包级别的变量 函数级别的变量 块级别的变量 包级别的变量 函数级别的变量 块级别的变量 子块级别的变量 */
注意:函数(块)内定义的变量一定要使用,否则会报错
package main import ( "fmt" ) // 包级别的变量 var packageVar = "包级别的变量" func main() { // 函数级别的变量 var funcVar string = "函数级别的变量" fmt.Println(packageVar, funcVar) { // 块级别的变量 var blockVar = "块级别的变量" fmt.Println(packageVar, funcVar, blockVar) { // 子块级别的变量 var innerBlockVar = "子块级别的变量" fmt.Println(packageVar, funcVar, blockVar, innerBlockVar) } // 在块的外面调用不到块内的变量 fmt.Println(innerBlockVar) } } /* # command-line-arguments .\block.go:32:15: undefined: innerBlockVar */
块即变量的作用域,会限制变量的使用范围
子块中若与父块中有相同的变量,父块中的变量值会被子块中的变量值所覆盖
package main import "fmt" func main() { var name string = "name变量初识值" fmt.Println(name) // 给`name`变量重新赋值 name = "name变量被重新赋值" fmt.Println(name) { name = "name变量在块中被重新赋值" } fmt.Println(name) } /* name变量初识值 name变量被重新赋值 name变量在块中被重新赋值 */
就是编程时用于标识的一种符号,具体有:(变量、常量、函数、包、接口等)