如何在golang中动态命名结构项?

I'm using redigo and ScanStruct is very useful. However it's obvious I'm trying to input urlhost and urlreq and these values are dynamically generated based on the actual uri of the user.

Obviously the code below doesn't work so how do I achieve what I want by being able to dynamically naming my struct so I can ScanStruct properly?

   func GetInfo(urlhost string, urlreq string) {


    type qwInfo struct {
         "s"+urlreq int
         "c"+urlreq []byte
         "t"+urlreq int
    }

    var QwInfo qwInfo

    c := pool.Get()
    defer c.Close()

    values, _ := redis.Values(c.Do("HMGET", urlhost, "s"+urlreq, "c"+urlreq, "t"+urlreq))

    redis.ScanStruct(values, &QwInfo);

Thanks in advance.

This is not the way, go behave you should use reflection instead

v := reflect.ValueOf(qwInfo).Elem().FieldByName(field) 

There are three available functions for scanning purpose on the redigo library: Scan(), ScanSlice(), ScanStruct().

The ScanStruct() cannot be used in your case because struct scheme need to be explicitly defined on compile time, it cannot be dynamic.

Use Scan() instead. Create some variables to retrieve the values, then after scanning process is done, put all of those variables into one map object.

Example:

c := pool.Get()
defer c.Close()

values, _ := redis.Values(c.Do("HMGET", urlhost, "s"+urlreq, "c"+urlreq, "t"+urlreq))

var s int = -1
var c []byte
var t int = -1
redis.Scan(values, &s, &c, &t))

qwInfo := map[string]interface{}{
    "s"+urlreq: s,
    "c"+urlreq: c,
    "t"+urlreq: t,
}