Basic Installation of Go and Writing Your First Program
Before diving into complex projects, you need to set up your local environment so you can run, build, and compile Go code. Once the environment is ready, we will write a simple "Hello World" application to test it out.
Part 1: Installing the Go Runtime
To build and execute Go programs, you must install the Go Runtime.
- Open your browser and navigate to
golang.org/dl. - Find the download link for your operating system (macOS, Windows, or Linux) and grab the installer.
- Run the installer and click through the standard installation prompts.
- Verify the installation: Open your computer's terminal and type the single word
go, then hit enter. You should see a long help message appear on the screen. Thisgocommand is the primary tool you will use to interact with the Go language.
Part 2: Configuring the Editor (VSCode)
While you can use any editor (like Atom or Sublime Text), Visual Studio Code (VSCode) is highly recommended because it offers some of the best built-in integration with Go.
- Download VSCode from
code.visualstudio.comand install it. - Open VSCode, navigate to the top menu bar, click on View, and select Extensions.
- Search for "Go" and install the extension named "Rich Go language support for Visual Studio".
- Important Step: To ensure the extension can successfully install its underlying command-line tools, you must completely quit and restart the VSCode editor.
- Open a new file and change the language mode in the bottom right corner to Go. A yellow prompt will appear saying "Analysis Tools Missing"—click Install to allow a terminal window to grab the final tools needed to analyze your code.
Part 3: Writing Your First Program
Now that the environment is ready, let's write a tiny application.
- Create a new folder on your computer called
Hello Worldand open this folder in your code editor. - Inside this directory, create a new file named
main.go. - Add the following code exactly as it appears. Ensure you use double quotes (not single quotes) around your strings:
package main
import "fmt"
func main() {
fmt.Println("Hi there")
}
Part 4: How to Run the Code
To run your project, open your terminal and navigate inside your Hello World directory. You can use the Go Command-Line Interface (CLI) to execute the code in two different ways:
go run main.go: This command takes your file, compiles it, and immediately executes the result. When you run this, you will instantly seeHi thereprinted on the screen.go build main.go: This command will only compile your program; it does not execute it automatically. Running this will spit out a runnable executable file namedmain(ormain.exeon Windows) directly into your folder. You can then execute that file manually.
Part 5: Breaking Down the Code
Even though this is a simple file, it reveals the fundamental structure of all Go programs.
package main: A package is a collection of common source code files. The namemainis sacred in Go; it specifically tells the compiler that you are making an executable package that will spit out a runnable file, rather than a reusable dependency library. Any time you make an executable package, it must contain a function calledmain.import "fmt": By default, your package is isolated. Theimportstatement gives your package access to functionality written in another package.fmt(short for "format") is a part of Go's Standard Library, and it is primarily used to print information out to the terminal.func main():funcis short for function. We declare a function by providing the keywordfunc, the function's name, a set of parentheses for arguments, and curly braces containing the body of our logic.