How to make a slice in Golang?

    0

    1

    good-practice
    Golang Snippets

    You can use the make() function to create a new slice in Golang. // Output: test

    Discussion at: https://devhubby.com/thread/how-to-make-a-slice-in-golang

    Shortcut: create

    import "fmt"
    
    package main
    
    func main() {
      slice := []string{"test", "test2"}
    
      // Output: test
      fmt.Println(slice[0])
    
      // or you can use make()
      slice2 := make([]int, 2)
    
      // Output: [0 0]
      fmt.Println(slice2)
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.