golang中for循环方法的差异

用for循环遍历字符串时,也有 byte 和 rune 两种方式.第一种试为byte,第二种rune。

golang中string rune byte 三者的关系https://blog.haohtml.com/archives/17646

package main
import (
	"fmt"
)

func main() {
	s := "abc汉字"
 
	for i := 0; i < len(s); i++ {
	fmt.Printf("%c,", s[i])
	}
	 
	fmt.Println()
	 
	for _, r := range s {
		fmt.Printf("%c\n", r)
	}

}

输出结果:

a,b,c,d,e,f,d,ä,¸,­,
a,b,c,æ,±,‰,å,­,—,
a
b
c
汉
字

14 thoughts on “golang中for循环方法的差异

  1. Pingback: useful reference

  2. Pingback: الربح من الانترنت

  3. Pingback: 바카라쿠폰

  4. Pingback: http://smartwindowsstore.com/usa/window-replacement-directory/

  5. Pingback: Silicide Powder

  6. Pingback: 온라인카지노

  7. Pingback: 안전토토사이트

  8. Pingback: ziemines padangos

  9. Pingback: con heo dat

  10. Pingback: กู้ หนี้ นอก ระบบ

  11. Pingback: اخبار

  12. Pingback: cbdque

  13. Pingback: uv direct printing

  14. Pingback: Polished Concrete Floors

Comments are closed.