一、指针(重点)
1.获取命令行参数
package main
import (
"os"
"fmt"
)
func main() {
list := os.Args
n := len(list)
fmt.Println("n = ", n)
for i := 0; i < n; i++ {
fmt.Printf("list[%d] = %s\n", i, list)
}
for i, data := range list {
fmt.Printf("list[%d] = %s\n", i, data)
}
args := os.Args
if args == nil || len(args) < 2 {
fmt.Println("err: xxx ip port")
return
}
ip := args[1]
port := args[2]
fmt.Printf("ip = %s, port = %s\n", ip, port)
}
2.导包
package main
/*
import "fmt"
import _ "os" //忽略此包
*/
/*
import (
"fmt"
_ "os"
)
*/
import . "fmt" // .导包的形式可以直接使用方法名字
func main() {
Println("--------")
}
3.init函数
import (
_"fmt"
)
_操作其实是引入该包,而不是直接使用包里面的函数,而是调用了包里面的init函数
4.指针
package main
import "fmt"
func main() {
var a int = 10
fmt.Printf("a = %d\n", a)
fmt.Printf("&a = %p\n", &a) //取地址a,就是a的指针
}
a = 10
&a = 0xc42001a070
package main
import "fmt"
func main() {
var a int = 10
fmt.Printf("a = %d\n", a)
fmt.Printf("&a = %p\n", &a) //取地址a,就是a的指针
var p *int
p = &a //指针变量指向谁,就把谁的地址赋值给指针变量
fmt.Printf("p = %v, &a = %v\n", p, &a)
*p = 666 //操作的是p所指向的内存
fmt.Printf("*p = %v, a = %v\n", *p, a)
}
a = 10
&a = 0xc42001a070
p = 0xc42001a070, &a = 0xc42001a070
*p = 666, a = 666
- 指针的默认值是nil,没有NULL常量
- 操作符“&”取变量地址,“*”通过指针访问目标对象
- 不支持指针运算,不支持“->”运算符,直接用“.”访问目标成员
如果想用指针但是不赋值的情况下只能用下面的方法
package main
import "fmt"
func main() {
var p1 *int
p1 = new(int)
fmt.Println("*p1 =", *p1)
p2 := new(int)
*p2 = 111
fmt.Println("*p2 =", *p2)
}
*p1 = 0
*p2 = 111
只需要用new()函数,无需担心内存的生命周期或怎么样将其删除,因为go的内存管理系统会帮助我们打理一切。
内存操作问题
- 值传递
package main
import "fmt"
func main() {
a, b := 10, 20
swap(a, b) //值传递
fmt.Printf("main: a = %d, b = %d\n", a, b)
}
//swap函数操作的并非是和main里面的a b同一块内存空间,只是把a和b的值给了函数的实参
func swap(i int, j int) {
i, j = j, i
fmt.Printf("swap: i = %d, j = %d\n", i, j)
}
swap: i = 20, j = 10
main: a = 10, b = 20
- 地址传递
package main
import "fmt"
func main() {
a, b := 10, 20
swap(&a, &b) //地址传递
fmt.Printf("main: a = %d, b = %d\n", a, b)
}
func swap(i *int, j *int) {
*i, *j = *j, *i
fmt.Printf("swap: *i = %d, *j = %d\n", *i, *j)
}
swap: *i = 20, *j = 10
main: a = 20, b = 10
二、数组
1.定义
package main
import "fmt"
func main() {
var a [5]int = [5]int{1, 2, 3, 4, 5}
fmt.Println("a = ", a)
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("b = ", b)
//部分初始化
c := [5]int{1, 2}
fmt.Println("c = ", c)
//指定每个元素的初始化
d := [5]int{2: 10, 4: 29}
fmt.Println("d = ", d)
}
a = [1 2 3 4 5]
b = [1 2 3 4 5]
c = [1 2 0 0 0]
d = [0 0 10 0 29]
2.迭代
package main
import "fmt"
func main() {
var a [5]int = [5]int{1, 2, 3, 4, 5}
for i := 0; i < len(a); i++ {
fmt.Printf("a[%d] =%d\n", i, a[i])
}
fmt.Printf("-----------\n")
for j, data := range a {
fmt.Printf("a[%d] =%d\n", j, data)
}
}
a[0] =1
a[1] =2
a[2] =3
a[3] =4
a[4] =5
-----------
a[0] =1
a[1] =2
a[2] =3
a[3] =4
a[4] =5
3.比较和赋值
package main
import "fmt"
func main() {
var a [5]int = [5]int{1, 2, 3, 4, 5}
var b [5]int = [5]int{1, 2, 3, 4, 5}
var c [5]int = [5]int{1, 2, 3}
fmt.Println("a == b", a == b)
fmt.Println("a == b", a == c)
var d [5]int
d = a
fmt.Println("d = ", d)
}
a == b true
a == b false
d = [1 2 3 4 5]