将字符串数组转换为字段名称

Newbie question: I want to print various variables of a library (is that the correct name? reflect.TypeOf(servers) gives []lib.Server)

I want to do something like this, but this obviously does not work:

servers, err := GetClient().GetServers() //call to external API

serverVariables := []string{}
serverVariables = append(serverVariables, "Name")
serverVariables = append(serverVariables, "IPAddress")

for _, server := range servers {
   for _,element := range serverVariables {
     fmt.Println(server.element)
   }
}

What I already can do is the following (but I want to do it using the above approach):

servers, err := GetClient().GetServers() //call to external API

for _, server := range servers {
   fmt.Println(server.Name)
   fmt.Println(server.IPAddress)
}

giving the following output:

ServerNameOne
192.168.0.1
ServerNameTwo
192.168.0.2

Reflection is what you probably want to use:

for _, server := range servers {
    v := reflect.ValueOf(server)
    for _, element := range serverVariables {
        fmt.Println(v.FieldByName(element))
    }
}

You should also change serverVariables initialization to be serverVariables := []string{}

Playground example: https://play.golang.org/p/s_kzIJ7-B7

It seems to me that you have experience in some dynamic language like Python or JavaScript. Go is compiled and strongly typed. Besides reflection being slower, when using it compiler can't help you with finding basic errors in your code and what is most important you lose type of the accessed variable. More info on http://blog.golang.org/laws-of-reflection

So I strongly recommend you to keep your current approach:

for _, server := range servers {
   fmt.Println(server.Name)
   fmt.Println(server.IPAddress)
}