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:trueandfalse) - 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 = 25var isActive bool = true
Method 2: Type Inference#
Go can infer the type from the value:
var name = string">"Gopher" string">"comment">// string type is inferredvar age = 25 string">"comment">// int type is inferredvar 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 := 25isActive := 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, 30string">"comment">// Or with type inferencevar a, b, c = string">"hello", 50, truestring">"comment">// Or with short declarationfirstName, 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: 0var f float64 string">"comment">// Zero value: 0.0var b bool string">"comment">// Zero value: falsevar s string string">"comment">// Zero value: string">"" (empty string)
Constants#
Constants are declared using the const keyword:
const Pi = 3.14159const (StatusOK = 200StatusNotFound = 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 = 42var f float64 = float64(i)var u uint = uint(f)
Example: Working with Different Types#
package mainimport string">"fmt"func main() {string">"comment">// Different ways to declare variablesvar a int = 10var b = 20c := 30string">"comment">// Zero valuesvar d intvar e stringvar f boolstring">"comment">// Constantsconst Pi = 3.14fmt.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 := 10y := 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).