3 回答
TA贡献1818条经验 获得超3个赞
使用反射包编写用于提取字段的“通用”函数:
func extractField(s interface{}, name string) []interface{} {
var result []interface{}
v := reflect.ValueOf(s)
for i := 0; i < v.Len(); i++ {
result = append(result, reflect.Indirect(v.Index(i)).FieldByName(name).Interface())
}
return result
}
像这样使用它:
logger.Debugf("personProcessor starting for people: %v", extractField(persons, "ID"))
该功能可以扩展到包括格式化花里胡哨的东西:
// sprintFields prints the field name in slice of struct s
// using the specified format.
func sprintFields(s interface{}, name string, sep string, format string) string {
var fields []string
v := reflect.ValueOf(s)
for i := 0; i < v.Len(); i++ {
fields = append(fields,
fmt.Sprintf(format,
reflect.Indirect(v.Index(i)).FieldByName(name).Interface()))
}
return strings.Join(fields, sep)
}
TA贡献1802条经验 获得超10个赞
请参阅此使用反射和字符串构建的函数:
func printPersonField(persons []Person, field string) {
buf := bytes.NewBuffer(nil)
fmt.Fprintf(buf, "Persons (%s): [", field)
for i := range persons {
if i > 0 {
fmt.Fprint(buf, ", ")
}
fmt.Fprint(buf, reflect.ValueOf(persons[i]).FieldByName(field).Interface())
}
fmt.Fprint(buf, "]")
log.Println(buf.String())
}
用法如下:
func main() {
persons := []Person{
{1, "first1", "last1"},
{2, "first2", "last2"},
{3, "first3", "last3"},
}
printPersonField(persons, "ID")
printPersonField(persons, "FirstName")
printPersonField(persons, "LastName")
}
输出:
2009/11/10 23:00:00 Persons (ID): [1, 2, 3]
2009/11/10 23:00:00 Persons (FirstName): [first1, first2, first3]
2009/11/10 23:00:00 Persons (LastName): [last1, last2, last3]
请注意,这不是“通用”,因为它不适用于任何类型。它严格接受输入。如果你想让它适用于其他类型的其他结构切片,那么请参阅我爱反射的答案。[]Person
TA贡献1797条经验 获得超6个赞
我不确定 Logrus,但这适用于标准库:
package main
import (
"fmt"
"strconv"
)
type Person struct {
ID int
FirstName, LastName string
}
func (p Person) String() string {
return strconv.Itoa(p.ID)
}
func main() {
p := Person{9, "First", "Last"}
fmt.Println(p) // 9
}
- 3 回答
- 0 关注
- 90 浏览
添加回答
举报