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.go182 chars10 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