-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add section12.1 section12.2 section12.3
- Loading branch information
张营磊
committed
Oct 18, 2023
1 parent
ab8ae12
commit 23ed932
Showing
3 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import "strconv" | ||
|
||
func Sprint(x interface{}) string { | ||
type stringer interface { | ||
String() string | ||
} | ||
|
||
switch x := x.(type) { | ||
case stringer: | ||
return x.String() | ||
case string: | ||
return x | ||
case int: | ||
return strconv.Itoa(x) | ||
// ...similar cases for int16, uint32, and so on... | ||
case bool: | ||
if x { | ||
return "true" | ||
} | ||
return "false" | ||
default: | ||
// array, chan, func, map, pointer, slice, struct | ||
return "???" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"reflect" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func main() { | ||
t := reflect.TypeOf(3) | ||
fmt.Println(t.String()) | ||
fmt.Println(t) | ||
|
||
var w io.Writer = os.Stdout | ||
t1 := reflect.TypeOf(w) | ||
fmt.Println(t1) | ||
|
||
fmt.Printf("%T\n", 3) | ||
|
||
v := reflect.ValueOf(3) | ||
fmt.Println(v) | ||
fmt.Printf("%v\n", v) | ||
fmt.Println(v.String()) | ||
fmt.Println(v.Type()) | ||
|
||
i := reflect.Value.Interface(v) | ||
fmt.Println(i) | ||
v.Interface() | ||
|
||
var x int64 = 1 | ||
var d time.Duration = 1 * time.Nanosecond | ||
fmt.Println(Any(x)) | ||
fmt.Println(Any(d)) | ||
fmt.Println(Any([]int64{x})) | ||
fmt.Println(Any([]time.Duration{d})) | ||
} | ||
|
||
// Any formats any value as a string. | ||
func Any(value interface{}) string { | ||
return formatAtom(reflect.ValueOf(value)) | ||
} | ||
|
||
func formatAtom(v reflect.Value) string { | ||
switch v.Kind() { | ||
case reflect.Invalid: | ||
return "invalid" | ||
case reflect.Int, reflect.Int8, reflect.Int16, | ||
reflect.Int32, reflect.Int64: | ||
return strconv.FormatInt(v.Int(), 10) | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, | ||
reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return strconv.FormatUint(v.Uint(), 10) | ||
// ...floating-point and complex cases omitted for brevity... | ||
case reflect.Bool: | ||
return strconv.FormatBool(v.Bool()) | ||
case reflect.String: | ||
return strconv.Quote(v.String()) | ||
case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Slice, reflect.Map: | ||
return v.Type().String() + " 0x" + | ||
strconv.FormatUint(uint64(v.Pointer()), 16) | ||
default: // reflect.Array, reflect.Struct, reflect.Interface | ||
return v.Type().String() + " value" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
type Movie struct { | ||
Title, Subtitle string | ||
Year int | ||
Color bool | ||
Actor map[string]string | ||
Oscars []string | ||
Sequel *string | ||
} | ||
|
||
func main() { | ||
Display("os.Stderr", os.Stderr) | ||
|
||
Display("rV", reflect.ValueOf(os.Stderr)) | ||
|
||
var i interface{} = 3 | ||
Display("i", i) | ||
Display("&i", &i) | ||
|
||
strangelove := Movie{ | ||
Title: "Dr. Strangelove", | ||
Subtitle: "How I Learned to Stop Worrying and Love the Bomb", | ||
Year: 1964, | ||
Color: false, | ||
Actor: map[string]string{ | ||
"Dr. Strangelove": "Peter Sellers", | ||
"Grp. Capt. Lionel Mandrake": "Peter Sellers", | ||
"Pres. Merkin Muffley": "Peter Sellers", | ||
"Gen. Buck Turgidson": "George C. Scott", | ||
"Brig. Gen. Jack D. Ripper": "Sterling Hayden", | ||
`Maj. T.J. "King" Kong`: "Slim Pickens", | ||
}, | ||
Oscars: []string{ | ||
"Best Actor (Nomin.)", | ||
"Best Adapted Screenplay (Nomin.)", | ||
"Best Director (Nomin.)", | ||
"Best Picture (Nomin.)", | ||
}, | ||
} | ||
Display("strangelove", strangelove) | ||
} | ||
|
||
func Display(name string, x interface{}) { | ||
fmt.Printf("Display %s (%T):\n", name, x) | ||
display(name, reflect.ValueOf(x)) | ||
} | ||
|
||
func display(path string, v reflect.Value) { | ||
switch v.Kind() { | ||
case reflect.Invalid: | ||
fmt.Printf("%s = invalid\n", path) | ||
case reflect.Slice, reflect.Array: | ||
for i := 0; i < v.Len(); i++ { | ||
display(fmt.Sprintf("%s[%d]", path, i), v.Index(i)) | ||
} | ||
case reflect.Struct: | ||
for i := 0; i < v.NumField(); i++ { | ||
fieldPath := fmt.Sprintf("%s.%s", path, v.Type().Field(i).Name) | ||
display(fieldPath, v.Field(i)) | ||
} | ||
case reflect.Map: | ||
for _, key := range v.MapKeys() { | ||
display(fmt.Sprintf("%s[%s]", path, | ||
formatAtom(key)), v.MapIndex(key)) | ||
} | ||
case reflect.Ptr: | ||
if v.IsNil() { | ||
fmt.Printf("%s = nil\n", path) | ||
} else { | ||
display(fmt.Sprintf("(*%s)", path), v.Elem()) | ||
} | ||
case reflect.Interface: | ||
if v.IsNil() { | ||
fmt.Printf("%s = nil\n", path) | ||
} else { | ||
fmt.Printf("%s.type = %s\n", path, v.Elem().Type()) | ||
display(path+".value", v.Elem()) | ||
} | ||
default: // basic types, channels, funcs | ||
fmt.Printf("%s = %s\n", path, formatAtom(v)) | ||
} | ||
} | ||
|
||
func formatAtom(v reflect.Value) string { | ||
switch v.Kind() { | ||
case reflect.Invalid: | ||
return "invalid" | ||
case reflect.Int, reflect.Int8, reflect.Int16, | ||
reflect.Int32, reflect.Int64: | ||
return strconv.FormatInt(v.Int(), 10) | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, | ||
reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return strconv.FormatUint(v.Uint(), 10) | ||
// ...floating-point and complex cases omitted for brevity... | ||
case reflect.Bool: | ||
return strconv.FormatBool(v.Bool()) | ||
case reflect.String: | ||
return strconv.Quote(v.String()) | ||
case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Slice, reflect.Map: | ||
return v.Type().String() + " 0x" + | ||
strconv.FormatUint(uint64(v.Pointer()), 16) | ||
default: // reflect.Array, reflect.Struct, reflect.Interface | ||
return v.Type().String() + " value" | ||
} | ||
} |