Go 反射机制介绍

yzhfd ·
最终代码的例子,输出结果是: insert into messages set iddfs=:iddfs, channelsdfs=:channelsdfs, user_name=:user_name, user_id=:user_id, user_avatar=:user_avatar, message=:message, message_raw=:message_raw, message_id=:message_id, stamp=:stamp 代码原意是不是应该是输出: insert into messages set id=1, channel=#common, user_name=titpetric, stamp=2019-03-11 17:00:24.2809257 +0800 CST m=+0.002991301, 对应代码为: sql := "insert into " + table + " set" for i := 0; i < len(message_fields); i++ { insVal := message_value.Field(i).Interface() if insVal != "" && insVal != "-" { tag := message_value.Type().Field(i).Tag.Get("db") sql = fmt.Sprintf("%s %s=%v, ", sql, tag, insVal) } }
#1