Golang reflect.Append() function and reflect.Value methods example
package reflect
Golang reflect.Append() function and reflect.Value methods usage example
package main
import (
"fmt"
"reflect"
)
func main() {
var str []string
var v reflect.Value = reflect.ValueOf(&str)
// make value to adopt element's type - in this case string type
v = v.Elem()
v = reflect.Append(v, reflect.ValueOf("abc"))
v = reflect.Append(v, reflect.ValueOf("def"))
v = reflect.Append(v, reflect.ValueOf("ghi"), reflect.ValueOf("jkl"))
fmt.Println("Our value is a type of :", v.Kind())
vSlice := v.Slice(0, v.Len())
vSliceElems := vSlice.Interface()
fmt.Println("With the elements of : ", vSliceElems)
}
Output :
Our value is a type of : slice
With the elements of : [abc def ghi jkl]
References :
http://golang.org/pkg/reflect/#Value.Slice
http://golang.org/pkg/reflect/#Value.Interface
http://golang.org/pkg/reflect/#Value.Len
Advertisement
Something interesting
Tutorials
+22.8k Golang : untar or extract tar ball archive example
+15.6k Golang : Get checkbox or extract multipart form data value example
+8k Golang : Handle Palindrome string with case sensitivity and unicode
+18.4k Golang : How to remove certain lines from a file
+5k Golang : Display packages names during compilation
+9.6k Golang : Read file with ioutil
+5.7k Linux/Unix/PHP : Restart PHP-FPM
+5.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+8.7k Golang : Combine slices but preserve order example
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+17k Golang : Get number of CPU cores
+5.7k Get website traffic ranking with Similar Web or Alexa