How to Create a Golang Project in VSCode

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.


Welcome to this advanced Golang tutorial on how to create a Golang project in VSCode. VSCode is a popular text editor that is widely used by developers for coding in different programming languages. Golang is a powerful language that is becoming increasingly popular in the development world. If you’re looking to learn how to create a Golang project in VSCode, you’re in the right place.

Before diving into the code examples, let’s first understand the theory behind creating a Golang project in VSCode.

In Golang, a project is a collection of related files that work together to achieve a specific task. A Golang project typically contains source code, configuration files, and dependencies. VSCode is an excellent tool for creating and managing Golang projects, as it provides a rich set of features and extensions that make it easier to write, debug, and test your code.

To create a Golang project in VSCode, we’ll need to follow a few simple steps. Let’s get started.

Step 1: Install Golang

Before we can create a Golang project in VSCode, we’ll need to install Golang on our computer. You can download and install Golang from the official Golang website.

Step 2: Install the Go extension for VSCode

To create a Golang project in VSCode, we’ll need to install the Go extension. This extension provides a set of tools for working with Golang, including code completion, debugging, and testing.

To install the Go extension, open VSCode and click on the Extensions icon on the left-hand side of the screen. Type “Go” into the search bar and click on the “Install” button next to the Go extension.

Step 3: Create a new Golang project

Once we’ve installed the Go extension, we can create a new Golang project in VSCode. To do this, we’ll need to follow these steps:

  1. Open VSCode and click on the File menu.
  2. Click on “New Folder”.
  3. Name your folder and press Enter.
  4. Open your newly created folder in VSCode.
  5. Open the terminal by pressing Ctrl+Shift+̀ or going to the View menu and selecting “Terminal”.
  6. Type “go mod init” followed by the name of your project. For example, “go mod init myproject”.

Step 4: Add files to your Golang project

Once we’ve created a new Golang project in VSCode, we can start adding files to it. We can add Golang source files, configuration files, and dependencies to our project.

To add a new Golang source file to our project, we can right-click on the project folder and select “New File”. We can then give our file a name and save it with the “.go” extension.

To add a new configuration file to our project, we can right-click on the project folder and select “New File”. We can then give our file a name and save it with the appropriate extension. For example, we might save our configuration file as “config.json” or “config.yml”.

To add dependencies to our project, we’ll need to use the “go get” command in the terminal. For example, we might type “go get github.com/gin-gonic/gin” to install the Gin web framework.

Step 5: Build and run your Golang project

Once we’ve added files and dependencies to our Golang project, we can build and run it using the VSCode built-in terminal. To do this, we’ll need to follow these steps:

  1. Open the terminal by pressing Ctrl+Shift+̀ or going to the View menu and selecting “Terminal”.
  2. Type “go build” to build your project.
  3. Type “./

Step 5: Build and run your Golang project

Once we’ve added files and dependencies to our Golang project, we can build and run it using the VSCode built-in terminal. To do this, we’ll need to follow these steps:

  1. Open the terminal by pressing Ctrl+Shift+̀ or going to the View menu and selecting “Terminal”.
  2. Type “go build” to build your project.
  3. Type “./<name_of_your_executable>” to run your project. For example, if your project is named “myproject”, you would type “./myproject”.

That’s it! You’ve successfully created a Golang project in VSCode and built and run it.

Now let’s take a look at some code examples to help solidify your understanding.

Code Example 1: Creating a simple Golang project

package main

import "fmt"

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

In this example, we’ve created a simple Golang program that prints “Hello, world!” to the console. To create this project in VSCode, we would follow the steps outlined above.

Code Example 2: Adding a configuration file to a Golang project

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type Config struct {
    Port int `json:"port"`
}

func main() {
    file, _ := os.Open("config.json")
    defer file.Close()

    decoder := json.NewDecoder(file)
    config := Config{}
    err := decoder.Decode(&config)
    if err != nil {
        fmt.Println("Error:", err)
    }

    fmt.Println("Port:", config.Port)
}

In this example, we’ve added a configuration file to our Golang project. The configuration file is in JSON format and contains a single property, “port”. We’ve created a struct to represent the configuration data and used the “encoding/json” package to decode the JSON data into our struct. To create this project in VSCode, we would follow the steps outlined above and create a new file named “config.json” in our project folder.

In conclusion, creating a Golang project in VSCode is a straightforward process that can be accomplished in just a few steps. With the help of the Go extension and the built-in terminal, we can easily build and run our projects, add new files and dependencies, and debug our code. I hope this tutorial has been helpful and has provided you with the knowledge and confidence to create your own Golang projects in VSCode. Happy coding!


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!