Golang reflect.MakeSlice() function example
package reflect
Golang reflect.MakeSlice() function usage example.
package main
import (
"fmt"
"reflect"
)
func main() {
var str []string // must be a slice type for reflect.MakeSlice() to work
var strValue reflect.Value = reflect.ValueOf(&str)
indirectStr := reflect.Indirect(strValue)
valueSlice := reflect.MakeSlice(indirectStr.Type(), 100, 1024) // otherwise, will panice
kind := valueSlice.Kind()
cap := valueSlice.Cap()
length := valueSlice.Len()
fmt.Printf("valueMap type is [%v] with capacity of %v bytes and length of %v .", kind, cap, length)
}
Output :
valueMap type is [slice] with capacity of 1024 bytes and length of 100 .
Reference :
Advertisement
Something interesting
Tutorials
+13.8k Golang : unknown escape sequence error
+9.6k Golang : Read file with ioutil
+10k CodeIgniter : Load different view for mobile devices
+46.2k Golang : Read tab delimited file with encoding/csv package
+8.9k Golang : Sort lines of text example
+13.6k Golang : Set image canvas or background to transparent
+14.2k Elastic Search : Mapping date format and sort by date
+7.1k Restart Apache or Nginx web server without password prompt
+13.3k Golang : Linear algebra and matrix calculation example
+26.1k Mac/Linux and Golang : Fix bind: address already in use error
+36.3k Golang : Convert(cast) int64 to string
+17.8k Golang : Defer function inside init()