Learn Go programming with simple, clear examples
CONTENTS
Learn Go through examples. Each section shows a concept with runnable code.
Click the “Run” button to execute examples and see output.
Go is statically typed. Declare variables with var
and specify the type.
var name string = "Alice"
var age int = 25
Numbers are numbers. Add. Subtract. Multiply. Divide.
var x int = 10
var y int = 3
var sum int = x + y
Calculate 15 * 4 and store it in a variable called result
with type int
.
Strings are text. Words. Letters. Characters.
var greeting string = "Hello"
var name string = "World"
var message string = greeting + " " + name
Create a string called sentence
that says “Go is great”.
Slices hold multiple items. Ordered. Indexed. Dynamic.
var fruits []string = []string{"apple", "banana", "orange"}
var firstFruit string = fruits[0]
Create a slice called colors
with type []string
containing “red”, “green”, and “blue”.
Functions do things. Take input. Return output. Typed.
func greet(name string) string {
return "Hello, " + name
}
var message string = greet("Alice")
Create a function called add
that takes two integers and returns their sum.
Loops repeat. For each item. Do something.
var numbers []int = []int{1, 2, 3, 4, 5}
for _, num := range numbers {
fmt.Println(num * 2)
}
Print each number from 1 to 5.
Conditions check. If true, do this. If false, do that.
var age int = 18
if age >= 18 {
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}
Check if a number is positive. Print “Positive” or “Negative”.
Structs group data. Fields. Properties. Organized.
type Person struct {
Name string
Age int
City string
}
var person Person = Person{
Name: "Alice",
Age: 25,
City: "New York",
}
fmt.Println(person.Name)
Create a struct called Book
with Title
and Author
fields.
Methods belong to types. Functions. Attached. Organized.
type Person struct {
Name string
}
func (p Person) Greet() string {
return "Hello, I'm " + p.Name
}
var alice Person = Person{Name: "Alice"}
fmt.Println(alice.Greet())
Create a struct called Car
with a Brand
field and a Start
method.
Errors happen. Check them. Handle them.
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("cannot divide by zero")
}
return a / b, nil
}
Create a function that returns an error if a number is negative.
Interfaces define behavior. Methods. Contracts. Promises.
type Writer interface {
Write([]byte) (int, error)
}
type ConsoleWriter struct{}
func (cw ConsoleWriter) Write(data []byte) (int, error) {
return fmt.Print(string(data))
}
Create an interface called Reader
with a Read
method.
Create a simple program. Use everything you learned.
type Calculator struct {
history []string
}
func (c *Calculator) Add(a, b int) int {
result := a + b
c.history = append(c.history, fmt.Sprintf("%d + %d = %d", a, b, result))
return result
}
func (c *Calculator) ShowHistory() {
for _, entry := range c.history {
fmt.Println(entry)
}
}
func main() {
calc := &Calculator{}
calc.Add(5, 3)
calc.Add(10, 20)
calc.ShowHistory()
}
Create your own calculator with multiply and divide methods.
Conclusion
You learned Go. Variables. Functions. Structs. Loops. Conditions.
Simple. Clear. Direct.
Go is powerful. You are ready.