Learn Golang Hello World Programming from Scratch

Author

Reads 1.1K

Computer Coding
Credit: pexels.com, Computer Coding

So you want to learn Golang from scratch? Let's start with the basics. Golang, also known as Go, is a statically typed, compiled language developed by Google.

The first step to writing a "Hello World" program in Golang is to install the Go SDK on your computer. This can be done by downloading the Go installer from the official Go website.

To write a "Hello World" program, you need to create a new file with a .go extension. The file name can be anything you like, but for simplicity, let's call it "hello.go".

In this file, you'll need to write the following code: `package main`, `import "fmt"`, `func main()`, and `fmt.Println("Hello, World!")`.

For more insights, see: Golang Go

Setting Up Environment

First, you need to set up your Go environment, and it's easier than you think. Try running the following commands to ensure everything's running smoothly.

You'll want to set up environment variables, which is key to working smoothly with Go. Here's what you need:

  • GOPATH: This is your workspace directory where all your Go code lives (by default, it's ~/go).
  • GOROOT: This is where Go itself is installed, usually set automatically.

Want to see how it's all set up? Just run go env in your terminal.

Writing Your First Program

Credit: youtube.com, Golang Hello World – Writing your first Go program

To start, every Go file must begin with the package name statement, which is used to provide code compartmentalization and reusability. The package name main is used here.

You should create a new directory for your project, which is a good practice. Each new project should have its own directory.

The package name statement should be followed by an import statement, which is used to import other packages. In our case, the fmt package is imported and will be used inside the main function to print text to the standard output.

You'll need to save your program as main.go in the newly created directory. This is the file where the main function will reside.

The main function should always reside in the main package and should be declared with the func keyword. The main function is a special function that is executed first when the program starts.

Here's a step-by-step guide to creating your first program:

  1. Create a new directory for your project.
  2. Save your program as main.go in the newly created directory.
  3. Run your program using the command "go run hello-world/main.go".

In the main function, you'll need to call the Println function of the fmt package to print the text to the standard output. The Println function is used to write text to the standard output, and its syntax is package.Function().

For more insights, see: Install Golang Package

Running Your Code

Credit: youtube.com, GO Programming 002: First program in Golang - Hello World!

To run your Go code, you need to compile it first. There are three ways to do this: go run, go build, and go install. You can see a list of these commands by typing "go help" in the command prompt.

The simplest way to compile and run your code is to use the "go run" command. For example, if you have a file called main.go inside a directory, you can run it by typing "go run main.go" in the terminal.

Go run works similar to go build, but it compiles the file to a temporary location and runs it from there. You can see the location where the file is compiled by running "go run --work".

If you want to output an executable file, you need to use the go build command. This will leave the executable file in the current directory, which you can run by typing its name in the command prompt.

Here's an interesting read: Golang Args

Credit: youtube.com, Hello World - Go Lang Programming Tutorial: Part 1

The third option is go install, which compiles your code and puts the executable file in the bin directory. If you have the bin path added in your environment variable, you can run the executable file from anywhere.

Here are the three commands summarized:

Program Explanation

In Go, every program starts with a package declaration, which is a collection of source files used to organize related code into a single unit.

The package name main is used here because we will need to declare the main() function, which can only be declared in a package named main.

To start a program, you need to declare the main package, which is done with the keyword package main. This is a special function that is executed first when the program starts.

The main function should always reside in the main package, and it's where your program starts running. This is where the program execution starts from.

Credit: youtube.com, Golang tutorial hello world

Here are the key components of the Go program:

  • package main: Declares the main package.
  • import "fmt": Imports the fmt package for formatting output.
  • func main(): Defines the main function where the program starts running.
  • fmt.Println(): Prints the message to the console.

The main function calls the Println() function of the fmt package, which prints the passed argument (your text) to the standard output together with a new line. This is where the magic happens, and your "Hello World" program comes to life.

Suggestion: Golang Func Type

Creating and Managing Files

In Go, you can create a new file by using the `os` package and the `Create` function, as shown in the "Writing to a File" example.

You can also use the `ioutil` package to write to a file, but `os` is a more modern and recommended approach.

When you create a file, you can specify its name and mode, such as `0644` for read and write permissions.

The `os` package also provides functions for checking if a file exists, such as `Stat`, which returns information about a file.

You can use the `os` package to delete a file by using the `Remove` function.

Take a look at this: Golang Os.writefile

Credit: youtube.com, This is the correct way to write and read files in Golang - Go basics

In Go, you can also use the `filepath` package to work with file paths and directories, such as joining paths with `Join`.

The `filepath` package is part of the `path` package, which provides functions for working with file paths.

You can use the `filepath` package to get the directory name of a file with `Base`.

In Go, you can use the `ioutil` package to read the contents of a file, but it's not recommended for large files.

The `os` package provides a more efficient way to read files with the `Open` function.

You can use the `os` package to read the contents of a file and then close the file with the `Close` function.

Explore further: Golang Use .env File

Build System Overview

Go has a built-in build system that makes it super easy to compile and run your code.

The go command is your best friend when it comes to building and running your Go programs. You can use it to compile and run your program all in one go with the `go run` command.

A fresh viewpoint: Go vs Golang

Credit: youtube.com, Hello World in Golang !!! | Installation | Go Build //Golang

Here are the different commands you can use with the go command:

  • go run: This command compiles and runs your program all in one go.
  • go build: This creates an executable file that you can run separately.
  • go install: This installs your binary into your $GOPATH/bin directory for easy access.

With these commands, you can focus more on coding and less on the nitty-gritty of compilation.

Wm Kling

Lead Writer

Wm Kling is a seasoned writer with a passion for technology and innovation. With a strong background in software development, Wm brings a unique perspective to his writing, making complex topics accessible to a wide range of readers. Wm's expertise spans the realm of Visual Studio web development, where he has written in-depth articles and guides to help developers navigate the latest tools and technologies.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.