Golang reflect.AppendSlice() function example
package reflect
Golang reflect.AppendSlice() function 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)
v = reflect.AppendSlice(v, reflect.ValueOf([]string{"mno", "pqr", "stu"}))
vSlice = v.Slice(0, v.Len())
vSliceElems = vSlice.Interface()
fmt.Println("After AppendSlice : ", vSliceElems)
}
Output :
Our value is a type of : slice
With the elements of : [abc def ghi jkl]
After AppendSlice : [abc def ghi jkl mno pqr stu]
Reference :
Advertisement
Something interesting
Tutorials
+26.7k Golang : How to check if a connection to database is still alive ?
+9.7k Golang : Eroding and dilating image with OpenCV example
+8.6k Golang : Progress bar with ∎ character
+24.1k Golang : Upload to S3 with official aws-sdk-go package
+4.4k Linux/MacOSX : Search and delete files by extension
+24.6k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+17.3k Golang : How to tell if a file is compressed either gzip or zip ?
+11.2k Google Maps URL parameters configuration
+15.9k Golang : Get file permission
+8.4k PHP : How to parse ElasticSearch JSON ?
+10.9k Nginx : TLS 1.2 support