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 181 views
1 answer 97 views
1 answer 226 views
226 views asked Sep 15, 2020 by avibootz
2 answers 241 views
241 views asked Aug 23, 2020 by avibootz
1 answer 177 views
...