-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-conversions.go
99 lines (81 loc) · 3.44 KB
/
type-conversions.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package variables
import (
"fmt"
)
func TypeConversion() {
/* Go is a statically typed language, which means
that the data type of a variable is known at
compile time. Once a variable is declared, it
cannot be assigned a value of another type. */
/* The following code will give an error
because the variable a is declared as an int
but we are trying to assign a float value to it */
// var a int = 10.5
/* Thus we need to convert the value to the
correct data type before assigning it to a variable */
/* The process of converting the value of
one data type to another data type
is called type conversion */
/* In Go, type conversion is done by
specifying the data type in which
we want to convert the value in parentheses
before the value */
/* The syntax for type conversion is:
<variable_name> = <data_type>(<value>) */
/* The expression T(v) converts the value v to the type T. */
fmt.Println()
fmt.Println("Type Conversion")
var a int = 65
// Convert int to float32
var c float32 = float32(a)
fmt.Println("Converted a to float32:", c)
// Convert float32 to int
var d int = int(c)
fmt.Println("Converted c to int:", d)
/*Rune is an alias for int32
and is used to represent Unicode code points
the following code will print the character
corresponding to the Unicode code point 65
which is 'A'*/
var e rune = rune(a)
fmt.Println("Converted a to string:", e)
// Add an example of a type conversion that is not possible in Go
var s string = "123"
// var i int = int(s) // This type conversion is not possible in Go
// fmt.Println("Converted s to int:", i)
// This will give an error because you cannot convert a string to an int directly
// You will have to convert the string to a rune first and then to an int
var i int = int(rune(s[0]))
// This will convert the first character of the string to a rune and then to an int
fmt.Println("Converted s to int:", i)
// some type conversions may result in loss of data
// for example, converting a float to an int
// will result in the loss of the decimal part
var f float32 = 10.5
var g int = int(f)
fmt.Println("Converted f to int:", g)
// float64 to float32 will also result in loss of data
var j float64 = 423243242234242232.342423424234234234234242342
var k float32 = float32(j)
fmt.Println("Converted j to float32:", k)
// converting an int to a float will not result in loss of data
// because the float data type can store integer values as well
var h int = 10
var i1 float32 = float32(h)
fmt.Println("Converted h to float32:", i1)
// converting a string to a float will result in an error
// because the string data type cannot store a float value
var l string = "10.5"
// var m float32 = float32(l) // This type conversion is not possible in Go
// fmt.Println("Converted l to float32:", m)
// This will give an error because you cannot convert a string to a float directly
// You will have to convert the string to a rune first and then to a float
var m float32 = float32(rune(l[0]))
// This will convert the first character of the string to a rune and then to a float
fmt.Println("Converted l to float32:", m)
// converting a float to a string will result in an error
// because the float data type cannot store a string value
// var n float32 = 10.5
// var o string = string(n) // This type conversion is not possible in Go
// fmt.Println("Converted n to string:", o)
}