Welcome to the Go Programming Course! This guide will walk you through the fundamental concepts of Go, from writing a basic "Hello, World!" program to working with packages and structs.
- Go installed on your computer
- A text editor or IDE (such as VSCode, GoLand, or Sublime Text)
go-project/
├── main/
│ └── main.go
├── variables/
│ └── variables.go
├── control_structures/
│ └── control_structures.go
├── functions/
│ └── functions.go
└── structs/
└── structs.go
- Create a directory:
mkdir go-hello-world cd go-hello-world
// main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
go run main.go
Create a directory for this example:
mkdir variables
cd variables
Create variables.go:
// variables.go
package main
import "fmt"
func main() {
age := 33
fullName := "Ivan Rodriguez"
isMarried := true
fmt.Printf("Hello %s, you are %v years old and is %v that you are married", fullName, age, isMarried)
Run your program:
go run variables.go
Create a directory for this example:
mkdir control
cd control
touch control.go:
// control.go
package main
import "fmt"
func main() {
age := 19
// If-Else
if age > 18 {
fmt.Println("You are old enough for an alcoholic drink")
} else {
fmt.Println("Sorry, you are too young")
}
// For-loop
for i := 1; i <= 5; i++ {
fmt.Printf("I am iteration # %v", i)
fmt.Println()
}
day := "Tuesday"
// Switch statements
switch day {
case "Monday":
fmt.Println("It's a boring Monday")
case "Tuesday":
fmt.Println("We are making progress, it's Tuesday")
default:
fmt.Println("It's another dat, hopefully weekend")
}
}
Run your program:
go run control_structures.go
Create a directory for this example:
mkdir functions
cd functions
Create functions.go:
// functions.go
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func swap(x, y string) (string, string) {
return y, x
}
func main() {
sum := add(5, 3)
fmt.Println("Sum:", sum)
a, b := swap("hello", "world")
fmt.Println("Swapped:", a, b)
}
Run your program:
go run functions.go
Create a directory for this example:
mkdir structs
cd structs
Create structs.go:
// structs.go
package main
import "fmt"
type Person struct {
firstName string
lastName string
age int
}
func (p Person) fullName() string {
return p.firstName + " " + p.lastName
}
func main() {
person := Person{firstName: "John", lastName: "Doe", age: 30}
fmt.Println("First Name:", person.firstName)
fmt.Println("Last Name:", person.lastName)
fmt.Println("Age:", person.age)
fmt.Println("Full Name:", person.fullName())
}
Run your program:
go run structs.go
Create a directory for your package:
mkdir mypackage
cd mypackage
Create mypackage.go:
// mypackage/mypackage.go
package mypackage
import "fmt"
func Hello() {
fmt.Println("Hello from mypackage!")
}
Create main.go to use your package:
// main.go
package main
import "mypackage"
func main() {
mypackage.Hello()
}
Run your program:
go run main.go
Happy coding!