golang 学习二:go程序编译运行与变量

go 程序的编译与运行

第一个go程序 'helloworld.go'

// 声明包名,可以是任意名字,但只有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' 可以直接先进行编译后再运行

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变量在块中被重新赋值

*/

标识符

概念

就是编程时用于标识的一种符号,具体有:(变量、常量、函数、包、接口等)

命名规则

  1. 构成元素:非空Unicode字符、下划线、数字,但注意不能以数字开头;
  2. 不能使用go语言中的关键字(25个);
    • 声明:import、package
    • 实体声明和定义:chan、const、func、interface、map、struct、type、var
    • 流程控制:break、case、continue、default、defer、else、fallthrough、for、go、goto、 if、range、return、select、switch
  3. 避免使用go语言预先设定的一些内置标识符;
    • 内置常量:true、false、nil、iota
    • 内置类型:bool、byte、rune、int、int8、int16、int32、int64、uint、uint8、unit16、 unit32、unit64、uintptr、float32、float64、complex64、complex128、string、error
    • 内置函数:make、len、cap、new、append、copy、close、delete、complex、real、 imag、panic、recover
    • 空白标识符:_
  4. 建议使用驼峰体;
  5. 区分大小写。
抟扶摇而上者九万里
原文地址:https://www.cnblogs.com/fengting0913/p/15646274.html