加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
reflect.go 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
楠木 提交于 2020-02-21 10:17 . cleanup: gosimple
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by a MIT License.
* You may find a license copy in project root.
*/
package etherscan
import (
"fmt"
"reflect"
"strconv"
)
// extractValue obtains string-formed slice representation via
// input. It only handles string, int, and their slice form, and
// panics otherwise.
func extractValue(input interface{}) (output []string) {
v := direct(reflect.ValueOf(input))
if v.Kind() == reflect.Slice {
length := v.Len()
output = make([]string, length)
for i := 0; i < length; i++ {
output[i] = valueToStr(v.Index(i))
}
} else {
output = make([]string, 1)
output[0] = valueToStr(v)
}
return
}
// valueToStr convert v into proper string representation
// Only handles int and string, panic otherwise.
func valueToStr(v reflect.Value) (str string) {
switch v.Kind() {
case reflect.String:
str = v.String()
case reflect.Int:
str = strconv.FormatInt(v.Int(), 10)
default:
panic(fmt.Sprintf("valueToStr: %v is of unexpected kind %q", v, v.Kind()))
}
return
}
// direct traverses the pointer chain to fetch
// the solid value
func direct(v reflect.Value) reflect.Value {
for ; v.Kind() == reflect.Ptr; v = v.Elem() {
// relax
}
return v
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化