capypad
0 day streak
go / beginner
Snippet

Basic Data Types and Type Conversion

Go is a statically typed language with explicit type declarations. The basic types include int for integers, float64 for decimal numbers, bool for true/false values, and string for text. Go does not allow implicit type conversion between different types - you must explicitly convert using the syntax destinationType(variable).

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main
 
import (
"fmt"
"reflect"
)
 
func main() {
var i int = 42
var f float64 = 3.14
var b bool = true
var s string = "Go"
 
fmt.Printf("int: %d, type: %s\n", i, reflect.TypeOf(i))
fmt.Printf("float: %.2f, type: %s\n", f, reflect.TypeOf(f))
fmt.Printf("bool: %t, type: %s\n", b, reflect.TypeOf(b))
fmt.Printf("string: %s, type: %s\n", s, reflect.TypeOf(s))
 
converted := float64(i)
fmt.Println("Converted int to float64:", converted)
}
Breakdown
1
var i int = 42
Integer variable holding whole numbers
2
var f float64 = 3.14
Float64 variable holding decimal numbers (64-bit precision)
3
var b bool = true
Boolean variable holding true or false
4
var s string = "Go"
String variable holding text sequences
5
float64(i)
Explicit type conversion from int to float64 - Go requires explicit casts