-
Notifications
You must be signed in to change notification settings - Fork 0
/
learning-types04.go
34 lines (25 loc) · 1.03 KB
/
learning-types04.go
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
/* Alta3 Research | Author: RZFeeser
Type transformations - string to integer */
package main
import (
"fmt"
"strconv" // built in package for string conversions
)
func main() {
// create a string via inference
s := "33"
// string to int
// see documentation @ https://pkg.go.dev/strconv#Atoi
i, err := strconv.Atoi(s)
// avoid nesting and check error immediately
// less nesting means less load on the compiler
if err != nil {
panic(err)
}
fmt.Printf("The type of s is %T and the value is %v\n", s, s) // the type of s followed by the value of s
fmt.Printf("The type of i is %T and the value is %v\n", i, i) // the type of i followed by the value of i
// transform an integer into a string
// see documentation @ https://pkg.go.dev/strconv#Itoa
st := strconv.Itoa(42) // We have transformed 42 into a string
fmt.Printf("The type of st is %T and the value is %v\n", st, st) // the type of st followed by the value of st
}