How to Create a Golang Project from Scratch

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


This article will guide you through the process of creating a Golang project from scratch, covering all aspects of setting up your development environment, writing code, testing, and deploying your application. Whether you are a beginner or an experienced developer, this article will provide you with a comprehensive understanding of how to create a Golang project.



Before we begin, it’s important to note that Go (also known as Golang) is a statically typed language developed by Google in 2009. It is designed for building scalable and efficient software, particularly for large-scale systems. In this article, we will focus on creating a basic Golang project using Visual Studio Code, but you can use any other IDE of your choice.

Step 1: Setting up the Development Environment

To start, you’ll need to have Go installed on your computer. You can download the latest version from the official Go website (https://golang.org/). Once installed, open a terminal or command prompt and run the following command to check if Go is properly installed:

go version

This should display the current version of Go you have installed on your system.

Next, we’ll need to set up our development environment. You can use any code editor or IDE of your choice, but for this article, we’ll be using Visual Studio Code (VSCode). Download and install VSCode from https://code.visualstudio.com/. Once installed, open VSCode and click on “Extensions” in the left-hand menu. Search for “Go” and install the Go extension.

Step 2: Creating a New Project

Now that we have our development environment set up, let’s create a new project. In VSCode, click on “File” > “New File” to open a new file. Name this file main.go. This will be the entry point for our program.

Inside main.go, add the following code:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, World!")
}

This is a basic Golang program that prints “Hello, World!” to the console. Save this file and click on “Run” in the top-right corner of VSCode to run your program. You should see “Hello, World!” printed to the console.

Step 3: Writing Code

Now that we have our basic project set up, let’s start writing code. In Go, all functions must be defined in a package. A package is a collection of source files and their dependencies. In this case, we’ll create a new package called “hello”. Create a new folder called hello inside your project directory and create a file called hello.go.

Inside hello.go, add the following code:

package hello

import (
    "fmt"
)

func SayHello(name string) {
    fmt.Println("Hello,", name, "!")
}

This function takes a string parameter called name and prints a message to the console using the fmt.Println() function. Save this file and return to the main.go file.

Inside main.go, add the following code:

package main

import (
    "fmt"
    "hello"
)

func main() {
    hello.SayHello("world")
}

This imports the hello package and calls the SayHello() function with a string parameter of “world”. Save this file and run your program again to see the output in the console. You should see “Hello, world!” printed to the console.

Step 4: Testing

Testing is an essential part of any programming project. In Go, you can use a built-in testing framework called go test. Create a new file called hello_test.go inside your project directory and add the following code:

package hello

import (
    "testing"
)

func TestSayHello(t *testing.T) {
    want := "Hello, world!"
    got := SayHello("world")

    if got != want {
        t.Errorf("got %q, want %q", got, want)
    }
}

This defines a new test function called TestSayHello() that takes a parameter of type *testing.T. This is used to report any errors or failures during the test. Inside this function, we define what we expect the output to be by assigning it to a variable called want. We then call the SayHello() function with a string parameter of “world” and assign its return value to a variable called got. Finally, we compare the actual output (got) to the expected output (want) using the if statement. If they are not equal, we report an error to the test framework using the t.Errorf() function.

To run this test, open a terminal or command prompt in your project directory and run the following command:

go test -v hello_test.go

This will run all tests in the hello package. You should see output similar to the following:

=== RUN   TestSayHello
--- PASS: TestSayHello (0.00s)
PASS
ok  	hello	0.012s

This shows that our test passed and we got the expected output.

Step 5: Deploying Your Application

Now that you have created your application, it’s time to deploy it. In Go, deployment is typically done using a build system like go build. To create an executable file, run the following command in your terminal or command prompt:

go build -o myapp main.go hello/*.go

This will create an executable file called myapp that contains all of the code from our project. You can run this file directly on the command line to start your application.

Conclusion

In this article, we have covered the basics of creating a Golang project from scratch. We started by setting up our development environment and then created a new project using Visual Studio Code. Next, we wrote code in the hello package and tested it using the built-in testing framework. Finally, we deployed our application using the go build command.

We hope this article has provided you with a comprehensive understanding of how to create a Golang project from scratch. Whether you are just starting out or an experienced developer looking to expand your skills, this guide should provide you with a solid foundation for building powerful and efficient software using Go.


Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!