go range

 
 
	//array or slic
	datas := []int{1, 2, 3}
	//datas := [...]int{1, 2, 3}
	//datas := &[...]int{1, 2, 3}
	for idx, v := range datas {
		fmt.Println(idx, v)
	}

	//If only the first iteration variable is present,
	//the range loop produces iteration values from 0 up to len(a)-1
	//and does not index into the array or slice itself
	var pdatas *[3]int
	//len(pdatas) == 3
	for idx, _ := range pdatas {
		fmt.Println(idx) // 0 1 2
	}

	//string, type of v is rune, byteIdx为UTF-8编码字节位置
	//如果有无效的编码则v返回0xFFFD, then?
	for byteIdx, v := range "中文" {
		fmt.Printf("%d %x\n", byteIdx, v)
	}
	//0 4e2d
	//3 6587

	//map
	m := map[int]string{1: "11", 2: "22"}
	for k, v := range m {
		fmt.Println(k, v)
	}

	//chan or <-chan
	ch := make(chan int)
	go func() {
		//1. if ch is nil, blocks forever
		//2. until the channel is closed
		for v := range ch {
			fmt.Println("recv:", v)
		}
	}()

	ch <- 1
	ch <- 2
	close(ch)

	//
	var testdata *struct {
		a *[7]int
	}
	for i, _ := range testdata.a {
		// testdata.a is never evaluated; len(testdata.a) is constant
		// i ranges from 0 to 6
		fmt.Println(i)
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值