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 204 views
1 answer 109 views
1 answer 230 views
230 views asked Sep 15, 2020 by avibootz
2 answers 251 views
251 views asked Aug 23, 2020 by avibootz
1 answer 186 views
...