go misc

//有点没点不一样
days1 := [...]string{"Sat", "Sun"} //array [2]string
days2 := []string{"Sat", "Sun"}    //slice []string

 

 

	//key为位置
	vowels := [...]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
	fmt.Println(vowels['a'])
	//未指定key则前面一个的位置+1
	vowels1 := [...]bool{'a': true, true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
	fmt.Println(vowels1['b'])
	//map
	vowelmap := map[int]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
	fmt.Println(vowelmap['a'])

 

 

//将a的第b位设置为0
a &^ b

 

	f := func(ss ...string) {
		fmt.Println(reflect.TypeOf(ss))	//[]string
	}
	ss := []string{"1", "2"}
	f(ss...) //传slice类型要加个尾巴

 空函数调用会panic

	f := (func(int))(nil)
	f(1) //panic

 

	fmt.Println("汉语")                       //UTF-8 input text
	fmt.Println(`汉语`)                       //UTF-8 input text as a raw literal
	fmt.Println("\u6c49\u8bed")             //the explicit Unicode code points
	fmt.Println("\U00006c49\U00008bed")     //the explicit Unicode code points
	fmt.Println("\xe6\xb1\x89\xe8\xaf\xad") //the explicit UTF-8 bytes

 

 byte    alias for uint8

 rune    alias for int32

 uint         either 32 or 64 bits    //和实现有关

 

zero value:

  false for booleans, 0 for integers, 0.0 for floats, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.

 

make:  slice map channel 

 

 

[...]*Point{{1.5, -3.5}, {0, 0}}  // same as [...]*Point{&Point{1.5, -3.5}, &Point{0, 0}}

 

 

	if 0 == []int{1, 2, 3}[0] {
	}
	type INTS []int
	//if 0 == INTS{1, 2, 3}[0] {}	//error
	if 0 == (INTS{1, 2, 3}[0]) { //ok
	}
	if (0 == INTS{1, 2, 3}[0]) { //ok
	}

 

a[x]

For a of pointer to array type:

 

  • a[x] is shorthand for (*a)[x] ???
	var b byte = "汉字"[0] //b不可赋值

	var m map[int]int = nil
	v, ok := m[0]
	fmt.Println(ok, v, m[0]) //false 0 0

	m = map[int]int{1: 1, 2: 2}
	v, ok = m[0]
	fmt.Println(ok, v, m[0], m[1]) //false 0 0 1

 

	len(a[low:high:max]) == high-low
	cap(a[low:high:max]) == max-low

 

	var x interface{} = 7
	x.(string)          //panic
	i, ok := x.(string) //"" false

 

	v := <-ch	//通讯失败会返回0值
	v, ok := <-ch

 

*T(p)        	// same as *(T(p))
(*T)(p)      	// p is converted to *T
<-chan int(c)    // same as <-(chan int(c))
(<-chan int)(c)  // c is converted to <-chan int
(func())(x)      // x is converted to func()
(func() int)(x)  // x is converted to func() int
func() int(x)    // x is converted to func() int

func()(x)        // function signature func() x

 

	int32(1) << 33	//error
	i := int32(1)
	i << 33 		//ok

 

	//字符串比较
	"ab" == "ab"
	"ab" != "ab"
	"ac" > "ab"

 

	//表达式的计算顺序通常从左到右
	//However, the order of those events compared to the evaluation and indexing of x and the evaluation of y is not specified.
	a := 1
	f := func() int { a++; return a }
	x := []int{a, f()}           // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified
	m := map[int]int{a: 1, a: 2} // m may be {2: 1} or {2: 2}: evaluation order between the two map assignments is not specified
	m2 := map[int]int{a: f()}    // m2 may be {2: 3} or {3: 3}: evaluation order between the key and the value is not specified

 

i := 0
i, x[i] = 1, 2  // set i = 1, x[0] = 2

 

// f returns 1
func f() (result int) {
	defer func() {
		result++
	}()
	return 0
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值