N
N
nano_e_t_42017-08-21 07:27:25
go
nano_e_t_4, 2017-08-21 07:27:25

How to access a structure field through a variable?

Hello everyone
Who knows how to refer to the structure field through a variable?
example:

type Example struct {
    field1     string
    field2     string
}

func main {
    myStruct := Example{field1: "test1", field2: "test1"}
    custom_field := "field1"
    fmt.Println(myStruct.custom_field)
}

in this form, of course, the code will not work, but I hope I outlined the main essence of my problem.
Or is it better not to do it at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid Nikolaev, 2017-08-21
@nano_e_t_4

Read about reflect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question