How to access individual characters of string using for range loop in GoLang 1.20
In this video we are going to generate source code for the GoLang Program, How to access individual characters of string using chatGPT.
After generating we will verify and run the generated code.
Important note on source code: only source code that are acceptable by the youtube description box will be displayed.
##############SOURCE CODE#########################
package main
import "fmt"
func main() {
str := "Hello, world!"
for _, char := range str {
fmt.Printf("Character: %c\n", char)
}
}
Below is the explanation for the program:
In this program, we define a string str with the value "Hello, world!".
We then use a range loop to iterate over each character of the string.
The range loop returns two values on each iteration: the index of the current character (which we don't need, so we use the blank identifier _),
and the character itself, which we assign to the variable char.
Inside the loop, we use the %c format specifier to print out the current character.
#go #goprogramming #golang #golangtutorial #golanguage