Welcome back to this Go programming series! In this video, we’ll explore variables and data types in Go.
Strings & Declare variable
To declare a variable in go. We type var
, then the name of the variable, followed by its type.
var animal1 string = "cat"
You can also create a variable without specifying the type. Go will then infer the type.
var animal2 = "dog"
There is a shorthand way to declare variables in Go. This can only be used in functions. If you are outside a function use var instead.
animal3 := "monkey"
You can skip giving the variable a value and Go will give it a default value. For strings Go will assign the empty string as a value.
var animal4 string
animal4 = "lion"
We can only change the value to the same type. We can’t for example give a string variable value 10.
Variables without an explicit value initial value are given their zero value.
0
for numeric types,false
for the boolean type, and""
(the empty string) for strings.
package main
import "fmt"
func main() {
var animal1 string = "cat"
var animal2 string = "dog"
animal3 := "monkey"
var animal4 string
animal4 = "lion"
var defaultNum int
var defaultBool bool
var defaultStr string
fmt.Println(animal1, animal2, animal3, animal4) // cat dog monkey lion
fmt.Println(defaultNum, defaultBool, defaultStr) // 0 false ""
}
Variable Declaration Block
Go has something called a Variable declaration block which lets you declare multiple variables together using a single var
keyword followed by parentheses.
var (
name string = "John"
age int = 30
address string = "123 Go Street"
)
It’s a neat way to group and initialize several variables at once, making your code cleaner and more organized.
Constans
In Go, constants are fixed values that you define with the const
keyword and cannot be changed after they are set. Constants can be character, string, boolean, or numeric values.
package main
import "fmt"
func main() {
const pi = 3.14
const port = 1337
fmt.Println("Pi: ", pi)
fmt.Println("Port: ", Port)
}
Data types
String type
Strings are immutable sequences of characters, declared using double quotes ("").
Int type
The way we use numbers can be split into two main groups: those that can be negative (signed) and those that can’t (unsigned). Each type of number has different sizes
Signed Integers (Can be Negative)
- int8: Very small numbers, from -128 to 127.
- int16: Small numbers, from -32,768 to 32,767.
- int32: Medium numbers, from -2,147,483,648 to 2,147,483,647.
- int64: Large numbers, from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- int: This one adjusts based on your computer’s architecture.
Unsigned Integers (Only Positive Numbers)
- uint8: Tiny numbers, from 0 to 255.
- uint16: Small numbers, from 0 to 65,535.
- uint32: Medium numbers, from 0 to 4,294,967,295.
- uint64: Large numbers, from 0 to 18,446,744,073,709,551,615.
- uint: Like
int
, this adjusts to your computer’s architecture.
In Go, the size of int
and uint
types is automatically optimised to match the architecture of your computer, either 32-bit or 64-bit
package main
import "fmt"
func main() {
var num1 int = 1992
var num2 int64 = 123456789
var num3 int8 = 128 // err to big max number is 127
var num4 uint = -100 // err can't be negative
fmt.Println(num1, num2, num3)
}
This will output an error for num3
since 128 is too big for int8. It only supports values between -128 to 127. It will also throw an error on num4
since we are not allowed to give it a negative value.
When you need an integer value you should use int
unless you have a specific reason to use a sized or unsigned integer type.
Float type
In Go, floating-point numbers are used to handle decimals and fractions. There are two main types:
-
float32: This type is good for when you need to save memory and don’t require high precision. It can handle about 7 decimal digits accurately, making it suitable for basic applications.
-
float64: This type offers more precision, up to about 15 decimal digits, which is better for scientific calculations or any scenario where accuracy is crucial.
Floats are essential for representing numbers that aren’t whole, like measurements, currency values, or percentages.
package main
import "fmt"
func main() {
var amount1 float32 = 99.84
var amount2 float64 = 109.5
amount3 := 56.7433 // When using shorthand it will default to float64
}
Using float64
in Go is preferred because it’s more accurate for numbers with decimals and helps avoid mistakes in calculations. It’s a safer choice for most tasks, especially since the extra memory usage is usually not a concern.
Boolean type
A boolean (or bool) is a simple type that holds only two values: true
or false
. It’s used to make decisions in your code, like checking conditions or controlling program flow with if-else statements.
package main
import "fmt"
func main() {
var isTrue bool = true
isFalse := false
fmt.Println(isTrue, isFalse)
}
Other basic types
// byte is an alias for uint8
// is used to represent a byte of data (values from 0 to 255).
// commonly used when dealing with raw binary data,
// like files or network connections.
var b byte = 255
// rune is an alias for int32
// is used to represent a Unicode code point.
// Useful when working with individual characters in a string.
var r rune = 'A'
// complex64 is a complex number with float32 real and imaginary parts.
// Used in arithmetic math
var c64 complex64 = 1 + 2i
// complex128 is a complex number with float64 real and imaginary parts.
// provides higher precision than complex64.
var c128 complex128 = 1.0 + 2.0i
In Go, byte
and rune
are alias types that provide clearer, more descriptive naming for existing types.
A complex type is a number with both a real and an imaginary part, represented as complex64
or complex128
, useful for performing complex arithmetic operations.
Conclusion
Full source code can be found here Github
In this tutorial, we learned about different types in Go, like strings, integers, floats, and booleans. We also talked about variables and how to use & declare them. I hope you enjoyed this tutorial, Thank you so much see you in the next episode.