How to Create and Run a Go Project

In this tutorial, we will teach you how to create and run a simple program in Go programming language.

Creating and running a Go project involves several steps. I'll provide you with a basic example to get you started. This example will create a simple Go program that prints "Hello, World!" to the console:

  1. Set Up a Workspace:
  2. Go follows a workspace structure. We assume you have already set up a workspace for Go projects. If you haven't, please set one up for Windows by following this guide.

    By convention, the Go workspace directory should have the following structure:

    your_workspace/
    ├── bin/
    ├── pkg/
    └── src/

    Create a src/ directory inside your workspace. The bin/ and pkg/ directories are created automatically.

  3. Create a Go Project:
  4. Inside the src/ directory of your workspace, create a new directory for your Go project. To demonstrate this example, I will create a project called my-project.

    cd go_workspace/src
    mkdir my-project
    
  5. Add Project Directory to IDE:
  6. Launch Visual Studio Code on your computer. Click on File in the top menu, and then select "Add Folder to Workspace". A file dialog will open. Navigate to the location of your project folder that you've created (For example, your-workspace/src/my-project), select it, and click the Add button. Your project folder is now added to Visual Studio Code, and you can view the project files and folders in the sidebar on the left.

  7. Create a Go Source File:
  8. Right-click within the project folder in the sidebar or anywhere within the open folder in the editor area. From the context menu, choose New File. Give your Go source file a name with a .go extension. For example, create a file named "myfile.go" with the following code:

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, World!")
    }
  9. Build and Run the Project:
  10. Open a Windows PowerShell, navigate to the project directory, and use the go run command to build and run your Go program:

    cd go_workspace/src/my-project
    go run .\myfile.go
    

    This will compile and execute your Go program, and you should see the "Hello, World!" message in the terminal.

    Congratulations, you've created and run a simple Go project! You can now expand on this foundation to build more complex applications in the Go programming language.