共計(jì) 496 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
在 Go 語言中,可以使用 range 關(guān)鍵字來遍歷字符串。下面是一個(gè)簡單的例子:
package main
import ("fmt"
)
func main() {str := "Hello, World!"
for index, char := range str {fmt.Printf("Character at index %d is %c\n", index, char)
}
}
在上面的例子中,我們使用 range 關(guān)鍵字遍歷字符串 str,并輸出每個(gè)字符的索引和字符本身。輸出結(jié)果為:
Character at index 0 is H
Character at index 1 is e
Character at index 2 is l
Character at index 3 is l
Character at index 4 is o
Character at index 5 is ,
Character at index 6 is
Character at index 7 is W
Character at index 8 is o
Character at index 9 is r
Character at index 10 is l
Character at index 11 is d
Character at index 12 is !
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完