How to print length and capacity of array 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 array 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() {
// Declare an array of integers with length 5 and capacity 10
arr := make([]int, 5, 10)
// Print the length and capacity of the array
fmt.Printf("Length: %d, Capacity: %d\n", len(arr), cap(arr))
}
Below is the explanation for the program:
In this program, we use the make() function to declare an array of integers with length 5 and capacity 10 using the syntax arr := make([]int, 5, 10).
We then use the built-in len() and cap() functions to print the length and capacity of the array respectively.
We use fmt.Printf() to format the output string using the %d verb for integers and print it to the console.
#go #goprogramming #golang #golangtutorial #golanguage