How to index characters in a Golang string?

前端 未结 9 1852
小鲜肉
小鲜肉 2020-12-04 09:15

How to get an \"E\" output rather than 69?

package main

import \"fmt\"

func main() {
    fmt.Print(\"HELLO\"[1])
}

Does Golang have funct

9条回答
  •  离开以前
    2020-12-04 10:10

    How about this?

    fmt.Printf("%c","HELLO"[1])
    

    As Peter points out, to allow for more than just ASCII:

    fmt.Printf("%c", []rune("HELLO")[1])
    

提交回复
热议问题