Go Package reflect: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=External= * https://golang.org/pkg/reflect/ =Internal= * Standard Library") |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | =External= | ||
* https://pkg.go.dev/strconv | |||
* | =Internal= | ||
* [[Go_Language_Modularization#reflect|Standard library]] | |||
=Overview= | |||
=<tt>reflect.TypeOf()</tt>= | |||
= | ==Getting the Type of Variable== | ||
<syntaxhighlight lang='go'> | |||
package main | |||
import ( | |||
"fmt" | |||
"reflect" | |||
) | |||
func main() { | |||
i := 10 | |||
f := 10.1 | |||
s := "blue" | |||
fmt.Println(reflect.TypeOf(i)) // returns a Type and will print 'int' | |||
fmt.Println(reflect.TypeOf(f)) // returns a Type and will print 'float64' | |||
fmt.Println(reflect.TypeOf(s)) // returns a Type and will print 'string' | |||
* | err := errors.New("some error") | ||
fmt.Println(reflect.TypeOf(err)) // returns a Type and will print '*errors.errorString' | |||
} | |||
</syntaxhighlight> |
Latest revision as of 01:43, 28 December 2023
External
Internal
Overview
reflect.TypeOf()
Getting the Type of Variable
package main
import (
"fmt"
"reflect"
)
func main() {
i := 10
f := 10.1
s := "blue"
fmt.Println(reflect.TypeOf(i)) // returns a Type and will print 'int'
fmt.Println(reflect.TypeOf(f)) // returns a Type and will print 'float64'
fmt.Println(reflect.TypeOf(s)) // returns a Type and will print 'string'
err := errors.New("some error")
fmt.Println(reflect.TypeOf(err)) // returns a Type and will print '*errors.errorString'
}