You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
940 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import "fmt"
/**
new与make的区别:
1. 二者都是用来做内存分配的。
2. make只用于slice、map以及channel的初始化返回的还是这三个引用类型本身
3. 而new用于类型的内存分配并且内存对应的值为类型零值返回的是指向类型的指针。
*/
func main() {
a := 10
b := &a
fmt.Printf("%v, %p\n", a, b)
fmt.Printf("a: %T, b: %T\n", a, b)
fmt.Printf("%v\n", *b)
modify1(a)
fmt.Printf("%v\n", a)
modify2(&a)
fmt.Printf("%v\n", a)
//Go中 引用类型的比变量不仅不要声明,还需要对其分配内存空间,否则将无法存储
//而值类型则不需要分配内存空间
//var m *int //未分配内存空间
m := new(int)
*m = 150
fmt.Println(*m)
//var mm map[string]int//为分配内存空间
mm := make(map[string]int)
mm["123"] = 1233
fmt.Println(mm)
}
func modify1(num int) {
num = 20
}
func modify2(num *int) {
*num = 100
}