Documentation ¶
Index ¶
- func FuncPkgFullName(val reflect.Value) string
- func FuncPkgName(val reflect.Value) string
- func GoFuncName(val reflect.Value) string
- func GoPtrTypeAndValue(obj interface{}) (reflect.Type, reflect.Value)
- func GoTypeAndValue(obj interface{}) (reflect.Type, reflect.Value, bool)
- func GoTypeName(typ reflect.Type) string
- func IsAnyKind(typ reflect.Type, kinds ...reflect.Kind) bool
- func IsComplex(typ reflect.Type) bool
- func IsFieldExported(f reflect.StructField) bool
- func IsFloat(typ reflect.Type) bool
- func IsMethodExported(m reflect.Method) bool
- func IsNumber(typ reflect.Type) bool
- func IsSigned(typ reflect.Type) bool
- func IsUnsigned(typ reflect.Type) bool
- func PanicIf(err error)
- func ParseFloat(val string, bitSize int) float64
- func ParseInt(s string, typ Integer) (resultValue interface{}, err error)
- type Array
- type BitSize
- type Bool
- type Complex
- type Exportable
- type Field
- type Float
- type Func
- type Integer
- type Interface
- type Invokable
- type Map
- type Member
- type Method
- type New
- type Number
- type NumberType
- type Slice
- type String
- type Struct
- type Tag
- type Taggable
- type Type
- type TypeConverter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FuncPkgFullName ¶
func FuncPkgName ¶
func GoFuncName ¶
func GoTypeName ¶
func IsFieldExported ¶
func IsFieldExported(f reflect.StructField) bool
func IsMethodExported ¶
func IsUnsigned ¶
func ParseFloat ¶
Types ¶
type Complex ¶
type Complex interface { Number ImaginaryData(val interface{}) interface{} RealData(val interface{}) interface{} }
type Exportable ¶
type Exportable interface {
IsExported() bool
}
type Field ¶
type Field interface { Member Taggable IsAnonymous() bool Type() Type CanSet() bool Set(instance interface{}, value interface{}) Get(instance interface{}) interface{} }
func ConvertGoField ¶
func ConvertGoField(f reflect.StructField) Field
type Invokable ¶
type Invokable interface {
Invoke(obj interface{}, args ...interface{}) []interface{}
}
type Member ¶
type Member interface { Exportable Name() string String() string }
type Method ¶
type Method interface { Member Invokable OutNum() int OutTypes() []Type InNum() int InTypes() []Type }
func ConvertGoMethod ¶
type String ¶
type String interface { Type New ToNumber(val string, number Number) (interface{}, error) ToInt(val string) int ToInt8(val string) int8 ToInt16(val string) int16 ToInt32(val string) int32 ToInt64(val string) int64 ToUint(val string) uint ToUint8(val string) uint8 ToUint16(val string) uint16 ToUint32(val string) uint32 ToUint64(val string) uint64 ToFloat32(val string) float32 ToFloat64(val string) float64 }
type Struct ¶
type Struct interface { Type New Fields() []Field FieldNum() int FieldsExported() []Field FieldExportedNum() int FieldsUnexported() []Field FieldUnexportedNum() int FieldsAnonymous() []Field FieldAnonymousNum() int Methods() []Method MethodNum() int Implements(i Interface) bool IsEmbedded(candidate Struct) bool }
type Type ¶
type Type interface { TypeConverter Name() string NameFull() string PkgName() string PkgNameFull() string PtrType() reflect.Type GoPtrValue() reflect.Value GoType() reflect.Type GoValue() reflect.Value IsPtr() bool IsInstantiable() bool String() string Equals(anotherType Type) bool }
func FromGoType ¶
func TypeOf ¶
func TypeOf(obj interface{}) Type
Example ¶
package main import ( "github.com/bingoohuang/gg/pkg/goo" ) type MyInterface interface { Method1(str string) Method2(val int, str string) } type MyStruct struct { Name string Price float32 } func MyFunc(string, int, MyStruct) (string, error) { return "test", nil } func main() { fn := goo.TypeOf(MyFunc) if fn.IsFunc() { fnType := fn.ToFuncType() outputs := fnType.Call([]interface{}{"test", 25, MyStruct{}}) if len(outputs) > 0 { // ... } } testStructType := goo.TypeOf(&MyStruct{}) if testStructType.IsStruct() { structType := testStructType.ToStructType() for _, method := range structType.Methods() { method.Name() // method.Invoke(...) method.IsExported() method.OutTypes() method.OutNum() method.InTypes() method.InNum() } // ... structType.FieldsExported() for _, field := range structType.Fields() { field.Name() field.Type() field.Get(&MyStruct{}) // field.Set(testStructInstance, nil) field.Tags() tag, err := field.TagByName("json") if err == nil { if tag.Value != "" && tag.Name != "" { // ... } } } } if testStructType.IsInstantiable() { structType := testStructType.ToStructType() newStructInstance := structType.New() if newStructInstance != nil { // ... } } testInterfaceType := goo.TypeOf((*MyInterface)(nil)) if testInterfaceType.IsInterface() { interfaceType := testInterfaceType.ToInterfaceType() interfaceType.Methods() // ... interfaceType.MethodNum() } testSignedIntType := goo.TypeOf(25) if testSignedIntType.IsNumber() { numberType := testSignedIntType.ToNumberType() if goo.IntType == numberType.Type() { integerType := numberType.(goo.Integer) if integerType.IsSigned() { // ... } } } testFloat32Type := goo.TypeOf(float32(42.28)) if testFloat32Type.IsNumber() { numberType := testFloat32Type.ToNumberType() if goo.FloatType == numberType.Type() { floatType := numberType.(goo.Float) if goo.Bit32 == floatType.BitSize() { // ... } } } testMapType := goo.TypeOf(make(map[string]bool, 0)) if testMapType.IsMap() { mapType := testMapType.ToMapType() keyType := mapType.KeyType() if keyType.IsString() { // ... } valueType := mapType.ValueType() if valueType.IsBool() { // ... } } if testMapType.IsInstantiable() { mapType := testMapType.ToMapType() newMapInstance := mapType.New() if newMapInstance != nil { // ... } } stringTestType := goo.TypeOf("test") if stringTestType.IsString() { stringType := stringTestType.ToStringType() stringType.ToUint8("20") stringType.ToUint64("58745") stringType.ToInt8("-23") stringType.ToUint64("9823") stringType.ToFloat32("23.52") stringType.ToFloat64("82387.32") } array := [5]string{} testArrayType := goo.TypeOf(array) if testArrayType.IsArray() { arrayType := testArrayType.ToArrayType() arrayType.ElemType() // ... arrayType.Len() } testSliceType := goo.TypeOf(array[2:]) if testSliceType.IsSlice() { sliceType := testSliceType.ToSliceType() sliceType.GetElementType() // ... sliceType.New() } }
Output:
type TypeConverter ¶
type TypeConverter interface { IsBool() bool ToBoolType() Bool IsNumber() bool ToNumberType() Number IsFunc() bool ToFuncType() Func IsStruct() bool ToStructType() Struct IsInterface() bool ToInterfaceType() Interface IsString() bool ToStringType() String IsMap() bool ToMapType() Map IsArray() bool ToArrayType() Array IsSlice() bool ToSliceType() Slice }
Click to show internal directories.
Click to hide internal directories.