The Building Blocks of a Program
To build any useful program, we need a way to store and manage information—a user's name, a score in a game, or the price of a product. In programming, we do this using variables and data types.
They are the most fundamental building blocks for handling data. Let's dive into how Go approaches this.
What Are Data Types? (Why Go is "Strict")
Before we can store a piece of data, we must tell Go what kind of data it is. Go is a statically-typed language, which means it's very strict about this rule.
Think of it like having labeled storage boxes: one for "Books", one for "Clothes", and one for "Toys". You can't put a toy in the book box. This strictness helps prevent many common bugs right from the start.
Here are the four most essential data types in Go:
-
string
: Used for text. Any sequence of characters enclosed in double quotes ("
).- Example:
"Hello, Budi"
,"123 Main St."
- Example:
-
int
: Used for integers (whole numbers), both positive and negative.- Example:
10
,-50
,2025
- Example:
-
float64
: Used for floating-point numbers (decimals).- Example:
3.14
,99.5
- Example:
-
bool
: Used for boolean logic (true
orfalse
).- Example:
true
,false
- Example:
Variables: The Containers for Your Data
A variable is simply a named container for our data. Once you create (declare) a variable, you can change its value later. There are two common ways to declare a variable in Go:
1. The Formal Way (var
)
This way is very explicit about the variable's type.
// var <variableName> <dataType> = <value>
var age int = 30
Go can also infer the type if you leave it out:
var city = "Jakarta" // Go knows this is a string
2. The Short Way (:=
)
This is the most common and preferred way to declare and initialize a variable inside a function. It's clean and concise. This is the most common and preferred way to declare and initialize a variable inside a function.
// <variableName> := <value>
name := "Siti"
isActive := true
Once a variable is declared, you can update its value using a simple equals sign (=
):
name = "Siti Nurbaya" // Updating the value
Constants: Values That Never Change
Sometimes, you have a value that you know will never, ever change throughout your program's life. Think of mathematical constants like Pi, or a specific configuration setting. For these, Go gives us constants.
A constant is declared using the const
keyword. Once set, its value is locked and cannot be changed.
const pi = 3.14
const defaultPort = 8080
// This would cause an error! You cannot change a constant.
// pi = 3.15
Putting It All Together: A Code Example
Let's see all these concepts in action in a single program. Create a main.go
file and try out this code:
package main
import "fmt"
func main() {
// --- STRINGS ---
// Using the short declaration (:=)
productName := "Go Programming E-book"
fmt.Println(productName)
// --- INTEGERS ---
// Using the formal declaration (var)
var quantity int = 10
var price = 50000 // Go infers this is an int
fmt.Println(quantity)
fmt.Println(price)
// --- FLOATS ---
var score float64 = 85.5
fmt.Println(score)
// --- BOOLEANS ---
isPublished := true
fmt.Println("Is the product published?", isPublished)
// --- CONSTANTS ---
const author = "Go Community"
fmt.Println("Author:", author)
// --- UPDATING A VARIABLE ---
fmt.Println("Current quantity:", quantity)
quantity = 15 // Update the value
fmt.Println("Updated quantity:", quantity)
}
Conclusion
You've now mastered the absolute fundamentals of handling data in Go. You know how to store text, numbers, and logical values using variables, and how to protect important values using constants.
These are the essential building blocks we'll use in every program from now on. In the next part of the series, we'll take a big step forward and learn how to store collections of data using two of Go's most powerful features: Slices and Maps. See you there!
Top comments (0)