Fundamentals
Program Structure
Go was created by Google engineers who were frustrated while waiting for a massive C++ codebase to compile. They wanted a language that felt as easy to write as Python, but ran and compiled incredibly fast!
To make an executable application, your code must live in a package called main and have a function called main().
main.go
package main // This tells Go "compile this into an executable file"
import "fmt" // "fmt" is the Format package from the standard library (used for printing)
func main() {
// 1. Variable (Short declaration)
serverName := "CloudServer-1"
// 2. Control Structure (The mighty 'for' loop)
for i := 1; i <= 3; i++ {
fmt.Println(serverName, "is booting up... Step", i)
}
}
Breakdown of what each part does:
package maintells the Go compiler that this file is a runnable application, rather than a reusable library.import "fmt"brings in the built-in"format"package, which gives you the tools needed to print text to the screen.func main()is the exact starting point where your program begins executing.