mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
fix: coding style and file format for all example.
This commit is contained in:
@@ -1,32 +1,33 @@
|
||||
// reflect1.go
|
||||
// blog: Laws of Reflection
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var x float64 = 3.4
|
||||
fmt.Println("type:", reflect.TypeOf(x))
|
||||
v := reflect.ValueOf(x)
|
||||
fmt.Println("value:", v)
|
||||
fmt.Println("type:", v.Type())
|
||||
fmt.Println("kind:", v.Kind())
|
||||
fmt.Println("value:", v.Float())
|
||||
fmt.Println(v.Interface())
|
||||
fmt.Printf("value is %5.2e\n", v.Interface())
|
||||
y := v.Interface().(float64)
|
||||
fmt.Println(y)
|
||||
}
|
||||
/* output:
|
||||
type: float64
|
||||
value: <float64 Value>
|
||||
type: float64
|
||||
kind: float64
|
||||
value: 3.4
|
||||
3.4
|
||||
value is 3.40e+00
|
||||
3.4
|
||||
*/
|
||||
// reflect1.go
|
||||
// blog: Laws of Reflection
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var x float64 = 3.4
|
||||
fmt.Println("type:", reflect.TypeOf(x))
|
||||
v := reflect.ValueOf(x)
|
||||
fmt.Println("value:", v)
|
||||
fmt.Println("type:", v.Type())
|
||||
fmt.Println("kind:", v.Kind())
|
||||
fmt.Println("value:", v.Float())
|
||||
fmt.Println(v.Interface())
|
||||
fmt.Printf("value is %5.2e\n", v.Interface())
|
||||
y := v.Interface().(float64)
|
||||
fmt.Println(y)
|
||||
}
|
||||
|
||||
/* output:
|
||||
type: float64
|
||||
value: <float64 Value>
|
||||
type: float64
|
||||
kind: float64
|
||||
value: 3.4
|
||||
3.4
|
||||
value is 3.40e+00
|
||||
3.4
|
||||
*/
|
||||
|
Reference in New Issue
Block a user