Golang Tutorial: Simple Values and Primitive Types
Every programming language relies on fundamental building blocks called simple or primitive values to represent raw data before binding them to variables or building complex data structures. In Go (Golang), understanding these core value types—integers, strings, booleans, and floating-point numbers—provides the necessary mental model for type evaluation, expression computation, and standard output rendering.
1. Why Start with Simple Values?
Before introducing variable declarations (var, :=), memory allocation, or complex composite types (structs, slices, maps), it is essential to understand how the Go compiler and runtime evaluate raw literals and basic expressions.
Key Concept: Value Literals
A literal is a source code representation of a fixed value. Go recognizes literals directly in the code without needing variable assignment:
- Numeric literals (e.g.,
1,10.5) - Textual literals (e.g.,
"hello golang") - Logical literals (
true,false)
2. Project Setup and File Structure
In Go, every executable standalone program begins with package main and defines an entry point function named main().
Directory Layout
Execution Command
To compile and run the source file in a single step during development:
go run 2_simple_values/main.go
3. Integer Values and Arithmetic Evaluation
Integers represent whole numbers (positive, negative, and zero) without fractional components.
package main
import "fmt"
func main() {
// Direct integer literal
fmt.Println(1)
// Arithmetic expression evaluation
fmt.Println(1 + 1)
}
Execution Flow & Behavior:
- When passing
1tofmt.Println(), Go prints the integer literal1. - When passing an expression like
1 + 1, the Go runtime evaluates the operator (+) between the two integer operands before passing the evaluated result (2) to the print function.
4. String Literals
Strings in Go represent sequences of text characters enclosed in double quotes ("...").
package main
import "fmt"
func main() {
// String literal
fmt.Println("hello golang")
}
Properties of Go Strings:
- Strings must be wrapped in double quotes (
"...") or backticks (`...`for raw multi-line strings). - Single quotes (
'...') are reserved forrune(individual Unicode code points/characters) and cannot be used for multi-character strings.
5. Boolean Values (bool)
Booleans represent binary truth values and form the basis of all conditional logic and control flow in Go programs.
package main
import "fmt"
func main() {
// Boolean true
fmt.Println(true)
// Boolean false
fmt.Println(false)
}
Key Characteristics:
- In Go, boolean is represented by the
booltype. - The only valid boolean literals are
trueandfalse(in lowercase). - Unlike languages like C or Python, Go does not treat non-zero integers as
trueor zero asfalse(no implicit truthy/falsy conversion).
6. Floating-Point Numbers and Division
Floats represent real numbers containing decimal points or fractional parts.
package main
import "fmt"
func main() {
// Direct floating-point literal
fmt.Println(10.5)
// Floating-point arithmetic (division)
fmt.Println(7.0 / 3.0)
}
Integer vs. Float Division Comparison:
Note: By default, untyped floating-point constants like
10.5or7.0are evaluated asfloat64in Go expressions, providing high-precision 64-bit IEEE 754 arithmetic.
7. Execution and Output Pipeline
When executing code with go run, the Go toolchain parses the literals, evaluates expressions, and streams the formatted text to standard output (stdout).
8. Summary Comparison of Simple Values
9. Complete Consolidated Code (main.go)
Below is the complete, self-contained demonstration file covering all the simple values discussed:
package main
import "fmt"
func main() {
// 1. Integers
fmt.Println(1)
fmt.Println(1 + 1)
// 2. Strings
fmt.Println("hello golang")
// 3. Booleans
fmt.Println(true)
fmt.Println(false)
// 4. Floats
fmt.Println(10.5)
fmt.Println(7.0 / 3.0)
}
Terminal Output:
1
2
hello golang
true
false
10.5
2.3333333333333335
10. Summary and Key Takeaways
- Foundational Building Blocks: Simple values (integers, strings, booleans, floats) are primitive literals used to construct all higher-level data in Go.
- Entry Point Requirement: Every runnable Go executable requires
package mainand afunc main()entry point. - Immediate Expression Evaluation: Go evaluates arithmetic expressions passed to functions like
fmt.Println()before outputting the result. - Strict Typing for Booleans: The
booltype strictly acceptstrueandfalse; integers cannot be implicitly cast to booleans. - Precision Matters in Division: Using explicit decimal points (
7.0 / 3.0) triggers floating-point division rather than integer truncation (7 / 3). - Standard Library Printing: The
fmtpackage provides formatted I/O operations, withfmt.Printlnappending a newline after printing the arguments.