简体   繁体   English

golang for 循环一个字符串,但它打印 'char' 为 int,为什么?

[英]golang for loop a string, but it prints 'char' as int, why?

A very simple go function:一个非常简单的 go function:

func genString(v string) {
    for _, c := range v {
        fmt.Println(c)
    }
}

Called in:调用:

func TestBasics(t *testing.T) {
    genString("abc")
}

Then I ran:然后我跑了:

go test -v -run TestBasics xxxxxx

It prints:它打印:

97
98
99

I expected that it should print我预计它应该打印

a
b
c

But it prints the corresponding integer value?但是它打印出相应的 integer 值? Why, how to fix it and print just the char ?为什么,如何修复它并只打印char

Thanks!谢谢!

Why为什么

Looping with range over string will give you a sequence of rune s.string上循环range将为您提供一系列rune s。

For range spec :对于range规格

Range expression                          1st value          2nd value

array or slice  a  [n]E, *[n]E, or []E    index    i  int    a[i]       E
string          s  string type            index    i  int    see below  rune
map             m  map[K]V                key      k  K      m[k]       V
channel         c  chan E, <-chan E       element  e  E

(notice the second row and last column in the table) (注意表格中的第二行和最后一列)

  1. For a string value, the "range" clause iterates over the Unicode code points in the string starting at byte index 0. On successive iterations, the index value will be the index of the first byte of successive UTF-8-encoded code points in the string, and the second value, of type rune , will be the value of the corresponding code point .对于字符串值,“range”子句从字节索引 0 开始迭代字符串中的 Unicode 代码点。在连续迭代中,索引值将是连续 UTF-8 编码代码点的第一个字节的索引rune类型的字符串和第二个值将是相应代码点的值 If the iteration encounters an invalid UTF-8 sequence, the second value will be 0xFFFD, the Unicode replacement character, and the next iteration will advance a single byte in the string.如果迭代遇到无效的 UTF-8 序列,则第二个值将是 0xFFFD,即 Unicode 替换字符,并且下一次迭代将在字符串中前进一个字节。

A rune value is an integer value identifying a Unicode code point . rune值是一个integer值,用于标识一个Unicode 代码点
The type itself is just an alias of int32 .类型本身只是int32的别名。


how to fix it and print just the char如何修复它并只打印字符

Use fmt.Printf with the %c verb to print the character value, ie fmt.Printf("%c\n", c)使用带有%c动词的fmt.Printf打印字符值,即fmt.Printf("%c\n", c)

fmt printing verbs doc : fmt打印动词文档

Integers:整数:

%b  base 2
%c  the character represented by the corresponding Unicode code point
%d  base 10
%o  base 8
%O  base 8 with 0o prefix
%q  a single-quoted character literal safely escaped with Go syntax.
%x  base 16, with lower-case letters for a-f
%X  base 16, with upper-case letters for A-F
%U  Unicode format: U+1234; same as "U+%04X"

(notice the second row in the table) (注意表中的第二行)


for _, c := range "abc" {
    fmt.Printf("%c\n", c)
}

https://go.dev/play/p/BEjJof4XvIk https://go.dev/play/p/BEjJof4XvIk

change the function gen string to `func genstring(v string) { for _, c:= range v { fmt.Println(string(c))将 function gen 字符串更改为 `func genstring(v string) { for _, c:= range v { fmt.Println(string(c))

  }

}` }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM