10 examples of 'golang typeof' in Go

Every line of 'golang typeof' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
13func typeof(x interface{}) string { return reflect.TypeOf(x).String() }
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

21func goType(t TypeInfo) reflect.Type {
22 switch t.Type() {
23 case TypeVarchar, TypeAscii, TypeInet:
24 return reflect.TypeOf(*new(string))
25 case TypeBigInt, TypeCounter:
26 return reflect.TypeOf(*new(int64))
27 case TypeTimestamp:
28 return reflect.TypeOf(*new(time.Time))
29 case TypeBlob:
30 return reflect.TypeOf(*new([]byte))
31 case TypeBoolean:
32 return reflect.TypeOf(*new(bool))
33 case TypeFloat:
34 return reflect.TypeOf(*new(float32))
35 case TypeDouble:
36 return reflect.TypeOf(*new(float64))
37 case TypeInt:
38 return reflect.TypeOf(*new(int))
39 case TypeDecimal:
40 return reflect.TypeOf(*new(*inf.Dec))
41 case TypeUUID, TypeTimeUUID:
42 return reflect.TypeOf(*new(UUID))
43 case TypeList, TypeSet:
44 return reflect.SliceOf(goType(t.(CollectionType).Elem))
45 case TypeMap:
46 return reflect.MapOf(goType(t.(CollectionType).Key), goType(t.(CollectionType).Elem))
47 case TypeVarint:
48 return reflect.TypeOf(*new(*big.Int))
49 case TypeTuple:
50 // what can we do here? all there is to do is to make a list of interface{}
51 tuple := t.(TupleTypeInfo)
52 return reflect.TypeOf(make([]interface{}, len(tuple.Elems)))
53 default:
54 return nil
55 }
56}
129func (d pgsqlDriver) typeOf(rv reflect.Value, autoIncrement bool) (string, error) {
130 switch rv.Kind() {
131 case reflect.Bool:
132 return "boolean", nil
133
134 case reflect.Float32, reflect.Float64:
135 return "numeric", nil
136
137 case reflect.Int8, reflect.Int16, reflect.Uint8:
138 if autoIncrement {
139 return "serial", nil
140 }
141 return "smallint", nil
142
143 case reflect.Int, reflect.Int32, reflect.Uint, reflect.Uint16, reflect.Uint32:
144 if autoIncrement {
145 return "serial", nil
146 }
147 return "integer", nil
148
149 case reflect.Int64, reflect.Uint64:
150 if autoIncrement {
151 return "bigserial", nil
152 }
153 return "bigint", nil
154
155 case reflect.String:
156 return "text", nil
157
158 case reflect.Struct:
159 if _, ok := rv.Interface().(time.Time); ok {
160 return "timestamp without time zone", nil
161 }
162
163 default:
164 if _, ok := rv.Interface().([]byte); ok {
165 return "bytea", nil
166 }
167 }
168
169 return "", ErrUnsupportedType
170}
48func getArrayItemsType(t *Type, typ string) string {
49 if typ == "array" {
50 return fmt.Sprint(t.Items)
51 }
52 return strings.TrimSuffix(typ, "[]")
53}
21func ElemTypeOf(i interface{}) reflect.Type {
22 if i == nil {
23 return nil
24 }
25
26 t := reflect.TypeOf(i)
27 k := t.Kind()
28
29 switch k {
30 case reflect.Array, reflect.Chan, reflect.Ptr, reflect.Slice:
31 return t.Elem()
32 default:
33 return t
34 }
35}
135func basicType(typ string) string {
136 switch typ {
137 case "", "String":
138 return "string"
139 case "Integer", "INTEGER":
140 return "int64"
141 case "Number", "NUMBER", "Double", "DOUBLE", "Float", "FLOAT":
142 return "float64"
143 case "Boolean", "BOOLEAN":
144 return "bool"
145 case "Menu", "MenuItem", "NativeImage", "WebRequest", "WebContents",
146 "Session", "Tray", "BrowserWindowProxy", "ClientRequest", "Cookies",
147 "DownloadItem", "BrowserWindow", "IncomingMessage":
148 return "*" + typ
149 }
150 return "*js.Object"
151}
82func (ru reflectionUtil) Type(obj interface{}) reflect.Type {
83 t := reflect.TypeOf(obj)
84 for t.Kind() == reflect.Ptr {
85 t = t.Elem()
86 }
87
88 return t
89}
275func typeOf(info *types.Info, x ast.Expr) types.Type {
276 tv := info.Types[x]
277 if tv.IsType() {
278 return nil
279 }
280 return tv.Type
281}
20func SimpleTypeName(t reflect.Type) string {
21 isPtr := t.Kind() == reflect.Ptr
22 elemType := t
23
24 if isPtr {
25 elemType = t.Elem()
26 }
27
28 out := elemType.Name()
29
30 switch elemType.Kind() {
31 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
32 out = "uint"
33 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
34 out = "int"
35 case reflect.Float32, reflect.Float64:
36 out = "float"
37 }
38
39 if isPtr {
40 out = "*" + out
41 }
42
43 return out
44}
1956func (b *float32Builder) GoType(typ interface{}) *float32Builder {
1957 b.desc.goType(typ, float32Type)
1958 return b
1959}

Related snippets