Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type MyStruct struct {
- FirstName [7]rune
- LastName [7]rune
- Organization [7]rune
- }
- func main () {
- // this is a line that needs to be mapped to MyStruct
- // its length is always 21 characters, 7 for each of the fields
- text := "John Smith Demo "
- runes := []rune(text)
- str := reflect.New(MyStruct).Elem()
- for i := 0; i < str.NumField(); i++ {
- // reuse the rune array length so we know when each field from the text ends
- end := str.Field(i).Len()
- // reflect.Set: value of type []int32 is not assignable to type [7]int32
- str.Field(i).Set(reflect.ValueOf(runes[:end]))
- runes = runes[end:]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement