Advertisement
Guest User

Untitled

a guest
Sep 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.62 KB | None | 0 0
  1. type MyStruct struct {
  2.     FirstName       [7]rune
  3.     LastName        [7]rune
  4.     Organization    [7]rune
  5. }
  6.  
  7. func main () {
  8.     // this is a line that needs to be mapped to MyStruct
  9.     // its length is always 21 characters, 7 for each of the fields
  10.     text := "John   Smith  Demo   "
  11.     runes := []rune(text)
  12.     str := reflect.New(MyStruct).Elem()
  13.     for i := 0; i < str.NumField(); i++ {
  14.         // reuse the rune array length so we know when each field from the text ends
  15.         end := str.Field(i).Len()
  16.         // reflect.Set: value of type []int32 is not assignable to type [7]int32
  17.         str.Field(i).Set(reflect.ValueOf(runes[:end]))
  18.         runes = runes[end:]
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement