1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| package main
import (
"fmt"
"strconv"
)
func main() {
i := 123
i64 := int64(i) // int to int64
f64 := float64(i) // int to float64
is := strconv.Itoa(i) // int to string
i64s := strconv.FormatInt(i64, 10) //int64 to string 10 進位
f64s := strconv.FormatFloat(f64, 'f', -1, 64) // float64 to string
f64i64 := int64(f64) // float64 to int64
letter := "abc" // string to byte
si, sierr := strconv.Atoi(is) // string to int
si64, si64err := strconv.ParseInt(is, 10, 64) // string to int64 [func ParseInt(s string, base int, bitSize int) (i int64, err error)]
sf64, sf64err := strconv.ParseFloat(is, 64) // string to float64 [func ParseFloat(s string, bitSize int) (f float64, err error)]
sb, sberr := strconv.ParseBool("true") // string to bool [func ParseBool(str string) (value bool, err error)]
fmt.Printf("i Type: %T, Value: %v\n", i, i)
fmt.Printf("i64 Type: %T, Value: %v\n", i64, i64)
fmt.Printf("f64 Type: %T, Value: %v\n", f64, f64)
fmt.Printf("is Type: %T, Value: %v\n", is, is)
fmt.Printf("i64s Type: %T, Value: %v\n", i64s, i64s)
fmt.Printf("f64s Type: %T, Value: %v\n", f64s, f64s)
fmt.Printf("f64i64 Type: %T, Value: %v\n", f64i64, f64i64)
fmt.Printf("letter Type: %T, Value: %v\n", []byte(letter), []byte(letter))
fmt.Printf("si Type: %T, Value: %v Err: %v\n", si, si, sierr)
fmt.Printf("si64 Type: %T, Value: %v Err: %v\n", si64, si64, si64err)
fmt.Printf("sf64 Type: %T, Value: %v Err: %v\n", sf64, sf64, sf64err)
fmt.Printf("sb Type: %T, Value: %v Err: %v\n", sb, sb, sberr)
}
/*
i Type: int, Value: 123
i64 Type: int64, Value: 123
f64 Type: float64, Value: 123
is Type: string, Value: 123
i64s Type: string, Value: 123
f64s Type: string, Value: 123
f64i64 Type: int64, Value: 123
letter Type: []uint8, Value: [97 98 99]
si Type: int, Value: 123 Err: <nil>
si64 Type: int64, Value: 123 Err: <nil>
sf64 Type: float64, Value: 123 Err: <nil>
sb Type: bool, Value: true Err: <nil>
*/
|