Install Go and Create a Simple Program
Course Title: Mastering Go: From Basics to Advanced Development Section Title: Introduction to Go and Development Environment Topic: Install Go and create a simple Go program.(Lab topic)
Objective: By the end of this lab, you will have gained hands-on experience in installing Go on your system and creating a simple Go program.
Step 1: Install Go
Before you can start writing Go programs, you need to install Go on your system. Here are the steps to install Go on different platforms:
- Windows:
- Download the Go installer from the official Go website: <https://go.dev/dl/>
- Run the installer and follow the installation prompts.
- Once the installation is complete, open a new command prompt and verify the installation by running the command
go version
.
- macOS (using Homebrew):
- Install Homebrew if you haven't already: <https://brew.sh/>
- Run the command
brew install go
to install Go. - Once the installation is complete, open a new terminal window and verify the installation by running the command
go version
.
- Linux:
- Download the Go tarball from the official Go website: <https://go.dev/dl/>
- Extract the tarball to a directory of your choice (e.g.,
~/go
). - Set the
GOROOT
environment variable by running the commandexport GOROOT=~/go
. - Add the Go
bin
directory to your system'sPATH
environment variable by running the commandexport PATH=$GOROOT/bin:$PATH
. - Verify the installation by running the command
go version
.
Step 2: Create a simple Go program
Now that you have Go installed on your system, let's create a simple Go program.
Create a new directory: Create a new directory for your Go project. For example, you can create a directory named
hello_world
in yourDocuments
directory:mkdir /Documents/hello_world
Create a new Go file: Create a new file named
main.go
inside thehello_world
directory. This file will contain your Go program.touch /Documents/hello_world/main.go
Write your Go program: Open the
main.go
file in a text editor and add the following code:```go package main
import "fmt"
func main() { fmt.Println("Hello, World!") }
**Explanation:**
* The `package main` declaration indicates that this file is the entry point of the program.
* The `import "fmt"` statement imports the `fmt` package, which provides functions for formatted I/O operations.
* The `func main()` declaration defines the `main` function, which is the entry point of the program.
* The `fmt.Println("Hello, World!")` statement prints "Hello, World!" to the console.
* **Run your Go program:**
To run your Go program, navigate to the `hello_world` directory in your terminal and run the following command:
```bash
go run main.go
This will compile and run your Go program. You should see the output "Hello, World!" in the console.
Conclusion:
In this lab, you installed Go on your system and created a simple Go program. You learned how to create a new directory, create a new Go file, write a Go program, and run the program using the go run
command.
Key Takeaways:
- Install Go on your system using the official Go installer or package managers like Homebrew.
- Create a new directory for your Go project and navigate to it in your terminal.
- Create a new Go file with a
.go
extension and write your Go program in it. - Use the
go run
command to compile and run your Go program.
What's Next: In the next topic, we will explore conditional statements in Go, including if-else statements and switch statements. We will learn how to use these statements to control the flow of our Go programs.
Do you have any questions or need help with installing Go or creating a simple Go program? Leave a comment below.
Images

Comments