golang get length of slice code example

Example 1: golang size of slice

// Create an exmaple array
    array := []int{1, 2, 3, 4, 5}
// Print number of items
    fmt.Println("First Length:", len(array))

Example 2: how to get the length of an array in go

array:=[]int {1,2,3,4}
len(array)

Example 3: golang get string length

package main

import "fmt"

func main() {
    value := "cat"

    // Take length of string with len.
    length := len(value)
    fmt.Println(length)

    // Loop over the string with len.
    for i := 0; i < len(value); i++ {
        fmt.Println(string(value[i]))
    }
}

Tags:

Go Example