Variables and Data Types in Go
Go is a statically typed language, which means variable types are checked at compile time. In this module, we'll explore Go's type system and how to work with variables.
Basic Types in Go#
Go has several built-in types:
- Numeric types:
- Integers:
int
,int8
,int16
,int32
,int64
,uint
,uint8
,uint16
,uint32
,uint64
- Floating point:
float32
,float64
- Complex:
complex64
,complex128
- Integers:
- Boolean type:
bool
(values:true
andfalse
) - String type:
string
(UTF-8 encoded text) - Error type:
error
Variable Declaration#
There are several ways to declare variables in Go:
Method 1: Explicit Type#
var name string = string">"Gopher"
var age int = 25
var isActive bool = true
Method 2: Type Inference#
Go can infer the type from the value:
var name = string">"Gopher" string">"comment">// string type is inferred
var age = 25 string">"comment">// int type is inferred
var isActive = true string">"comment">// bool type is inferred
Method 3: Short Variable Declaration#
Inside functions, you can use the short declaration operator (:=
):
name := string">"Gopher"
age := 25
isActive := true
This is the most common form used inside functions.
Multiple Variable Declaration#
You can declare multiple variables at once:
var x, y, z int = 10, 20, 30
string">"comment">// Or with type inference
var a, b, c = string">"hello", 50, true
string">"comment">// Or with short declaration
firstName, lastName, age := string">"Go", string">"pher", 25
Zero Values#
Variables declared without an explicit initial value are given their zero value:
var i int string">"comment">// Zero value: 0
var f float64 string">"comment">// Zero value: 0.0
var b bool string">"comment">// Zero value: false
var s string string">"comment">// Zero value: string">"" (empty string)
Constants#
Constants are declared using the const
keyword:
const Pi = 3.14159
const (
StatusOK = 200
StatusNotFound = 404
)
Constants can be character, string, boolean, or numeric values.
Type Conversion#
Go doesn't automatically convert between types. You must explicitly convert:
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
Example: Working with Different Types#
package main
import string">"fmt"
func main() {
string">"comment">// Different ways to declare variables
var a int = 10
var b = 20
c := 30
string">"comment">// Zero values
var d int
var e string
var f bool
string">"comment">// Constants
const Pi = 3.14
fmt.Println(string">"a:", a)
fmt.Println(string">"b:", b)
fmt.Println(string">"c:", c)
fmt.Println(string">"d (zero value):", d)
fmt.Println(string">"e (zero value):", e)
fmt.Println(string">"f (zero value):", f)
fmt.Println(string">"Pi:", Pi)
}
Type Checking in Action#
Go's type system helps catch errors at compile time:
x := 10
y := string">"hello"
z := x + y string">"comment">// This will cause a compile-time error
The above code won't compile because Go doesn't allow adding an integer and a string.
Exercise#
- Create a program that declares variables of different types
- Try converting between different types
- Experiment with zero values and constants
In the next module, we'll look at control structures in Go (if statements, loops, and switches).