Golang 基础知识易忽略点(命令行参数,常用格式化参数,数组与切片的参数传递,结构体的嵌入和匿名成员)

命令行参数

os.Args的第一个元素:os.Args[0],是命令本身的名字;其它的元素则是程序启动时传给它的参数

func main() {
	for index, item := range os.Args {
		fmt.Printf("index=%d,args=%s\n", index, item)
	}
}

go run os_args/main.go xxa a2 s3 d4
index=0,args=/tmp/go-build2136462296/b001/exe/main
index=1,args=xxa
index=2,args=a2
index=3,args=s3
index=4,args=d4

Printf 格式化

%d          十进制整数
%x, %o, %b  十六进制,八进制,二进制整数。
%f, %g, %e  浮点数: 3.141593 3.141592653589793 3.141593e+00
%t          布尔:true或false
%c          字符(rune) (Unicode码点)
%s          字符串
%q          带双引号的字符串"abc"或带单引号的字符'c'
%v          变量的自然形式(natural format)
%T          变量的类型
%p          十六进制表示,前缀 0x
%%          字面上的百分号标志(无操作数)

数组作为参数传递

当调用一个函数的时候,函数的每个调用参数将会被赋值给函数内部的参数变量,所以函数参数变量接收的是一个复制的副本,并不是原始调用的变量。因为函数参数传递的机制导致传递大的数组类型将是低效的,并且对数组参数的任何的修改都是发生在复制的数组上,并不能直接修改调用时原始的数组变量。

func main() {
	r := [...]int{99: -1}
	fmt.Printf("array r : %v\n", r)
	fmt.Printf("r:%p\n", &r)
	copyArray(r)
	fmt.Printf("array r : %v\n", r)
}

func copyArray(copyR [100]int) {
	fmt.Printf("copyR:%p\n", &copyR)
	fmt.Printf("array copyR : %v\n", copyR)
	copyR[0] = 100
	fmt.Printf("change array copyR : %v\n", copyR)
}

go run array/main.go                
array r : [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]
r:0xc00007c000
copyR:0xc00007c700
array copyR : [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]
change array copyR : [100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]
array r : [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]

Slice

slice值包含指向第一个slice元素的指针,因此向函数传递slice将允许在函数内部修改底层数组的元素(依然是拷贝,但是拷贝的对象持有原来的指针,对指针所指向值得修改将会影响到原值)

func main() {
	r := []int{99: -1}
	fmt.Printf("array r : %v\n", r)
	fmt.Printf("r:%p\n", &r)
	copyArray(r)
	fmt.Printf("array r : %v\n", r)
}

func copyArray(copyR []int) {
	fmt.Printf("copyR:%p\n", &copyR)
	fmt.Printf("array copyR : %v\n", copyR)
	copyR[0] = 100
	fmt.Printf("change array copyR : %v\n", copyR)
}

go run slice/main.go               
array r : [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]
r:0xc00000c030
copyR:0xc00000c060
array copyR : [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]
change array copyR : [100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]
array r : [100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1]

结构体

要在函数内部修改结构体成员的话,需要用指针传入,因为在Go语言中,所有的函数参数都是值拷贝传入的,函数参数将不再是函数调用时的原始变量

结构体嵌入和匿名成员

结构体的嵌入,访问属性麻烦

type Point struct {
	X, Y int
}

type Circle struct {
	Center Point
	Radius int
}

type Wheel struct {
	Circle Circle
	Spokes int
}

func main() {
	var w Wheel
	w.Circle.Center.X = 8
	w.Circle.Center.Y = 8
	w.Circle.Radius = 5
	w.Spokes = 20
}

匿名成员

type Point struct {
	X, Y int
}

type Circle struct {
	Point
	Radius int
}

type Wheel struct {
	Circle
	Spokes int
}

func main() {
	var w Wheel
	//version 1
	w.Circle.Point.X = 8
	w.Circle.Point.Y = 8
	w.Circle.Radius = 5
	w.Spokes = 20

	//version 2
	w.Circle.X = 9
	w.Circle.Y = 9
	w.Circle.Radius = 10
	w.Spokes = 10

	//version 3
	w.X = 1
	w.Y = 2
	w.Radius = 3
	w.Spokes = 4

	//结构体字面值必须遵循形状类型声明时的结构
	w = Wheel{Circle{Point{1, 1}, 5}, 20}

	w = Wheel{
		Circle: Circle{
			Point:  Point{X: 1, Y: 1},
			Radius: 5,
		},
		Spokes: 20,
	}
}
上一篇:13. Flex 弹性布局2 BootStrap


下一篇:麦森数