How to create slice using new keyword in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"reflect"
)

func main() {
	var slice = new([10]int)[0:5]

	fmt.Println(reflect.ValueOf(slice).Kind())
	fmt.Printf("Len: %v\n", len(slice))
	fmt.Printf("Cap: %v\n", cap(slice))
	fmt.Println(slice)
}




/*
run:

slice
Len: 5
Cap: 10
[0 0 0 0 0]

*/

 



answered Aug 23, 2020 by avibootz

Related questions

1 answer 176 views
1 answer 92 views
1 answer 223 views
223 views asked Sep 15, 2020 by avibootz
2 answers 229 views
229 views asked Aug 23, 2020 by avibootz
1 answer 170 views
1 answer 150 views
150 views asked Mar 14, 2020 by avibootz
...