How to print length and capacity of runes in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to print length and capacity of runes 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() {
// Define a slice of runes
runeSlice := []rune{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'}
// Print the length and capacity of the slice
fmt.Println("Length:", len(runeSlice))
fmt.Println("Capacity:", cap(runeSlice))
}
##############END OF SOURCE CODE#########################
Below is the explanation for the program:
In this program, we define a slice of runes called runeSlice containing the Unicode code points of the characters 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', and '!'.
We then print the length and capacity of the slice using the built-in len() and cap() functions respectively.
The len() function returns the number of elements in the slice, while the cap() function returns the capacity of the slice, which is the maximum number of elements that the slice can hold before it needs to be resized.
#go #goprogramming #golang #golangtutorial #golanguage