find the first character in a string in go

To find the first character of a string in Go, you can use indexing with square brackets [] and the index number 0, since Go is a 0-indexed language. Here is an example code snippet:

main.go
package main

import "fmt"

func main() {
    myString := "Hello, World!"
    firstChar := myString[0]
    fmt.Printf("The first character in \"%s\" is '%c'", myString, firstChar)
}
182 chars
10 lines

Output: The first character in "Hello, World!" is 'H'

In this example, we first declare a string variable myString with the value "Hello, World!". Then we use square brackets to get the character at index 0, and assign it to the variable firstChar. Finally, we print out the result using the Printf function, using the %c format specifier to print the character value.

gistlibby LogSnag