Welcome to your Go journey at CodeYourCraft! In this lesson, we'll write our first Go program and explore the basics of this powerful and popular programming language. By the end, you'll have a solid understanding of how Go works, ready to tackle more complex projects.
Go, also known as Golang, is an open-source programming language created by Google in 2009. Go is designed to be easy to learn, efficient, and safe for building simple and large-scale applications.
Before we dive into our first program, let's make sure you have Go installed on your computer. Follow the official Go installation guide to install Go on your system.
Now that Go is installed, let's write our first program!
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Let's break down the code above:
package main: This line declares that our program belongs to the main package, which is the entry point for every Go program.
import "fmt": We import the fmt package, which contains functions for printing output to the console.
func main(): This function is the main function of our program and is automatically called when we run our program.
fmt.Println("Hello, World!"): This line prints the string "Hello, World!" to the console using the Println function from the fmt package.
To run the program, save it to a file with a .go extension (e.g., main.go). Open a terminal, navigate to the directory containing your file, and run the following command:
go run main.goIf everything is set up correctly, you should see "Hello, World!" printed to the console. Congratulations! You've written your first Go program.
Now that you've written your first Go program, it's time to explore more. In the next sections, we'll dive deeper into Go concepts, such as variables, functions, and concurrency.
What is the purpose of the `package main` line in Go programs?
What is the purpose of the `fmt` package in Go programs?