Golang Init Project: From Setup to Execution

Author

Reads 553

IT professional working on a computer in a modern office setting, focused on coding and tasks.
Credit: pexels.com, IT professional working on a computer in a modern office setting, focused on coding and tasks.

To start a new Golang project, you can simply run the command `go mod init project-name` in your terminal, where "project-name" is the desired name for your project. This will create a new directory for your project and initialize a Go module.

The `go mod init` command is used to create a new module for your project. A Go module is a package that can be used by other Go programs, and it's essentially a directory that contains your project's source code.

By running `go mod init project-name`, you're creating a new module with the specified name, which will be used to manage your project's dependencies and versioning. This is an important step in setting up your project.

With your project initialized, you can start writing your Golang code. The next step is to create a `main.go` file, which is the entry point for your program. You can do this by running the command `touch main.go` in your terminal.

Additional reading: Golang Test Command

Project Setup

Credit: youtube.com, This Is The BEST Way To Structure Your GO Projects

To start a new Go project, you'll first create an empty folder. Then, you type `go mod init` in this folder to initialize the Go modules, which are used for your dependencies.

To initialize a Go project with "go-blueprint", you'll need to follow these steps. Install go-blueprint with the command `go get -u github.com/alexellis/go-blueprint`, then open the "go-blueprint" tool on the browser with the link provided.

Here are the basic steps to initialize a Go project with "go-blueprint":

  • Install go-blueprint with the command `go get -u github.com/alexellis/go-blueprint`
  • Open the "go-blueprint" tool on the browser with the link provided
  • Fill in the necessary information of the project into the form
  • Copy the command, paste it into the terminal and press enter

To start a new Go project with modules, you can initialize your module with a unique name by running the command `go mod init github.com/your-username-or-organization/my-new-go-project`, replacing the path with your desired module path.

On a similar theme: New Nextjs Project Typescript

Configs

Configs are an essential part of any project setup. You can find configuration file templates or default configs in the /configs directory.

This is where you can store your confd or consul-template template files. It's a good idea to keep them organized and easily accessible.

Remember to put your custom config files here, so they're easily found and updated. This will save you time and effort in the long run.

A fresh viewpoint: Golang Html Template

Setup Environment

Credit: youtube.com, Project setup & Environment

To start a project, you first need a good setup for your development environment.

We use Chocolatey on Windows to install Go and Visual Studio Code. I prefer using Chocolatey for its ease of installation.

With Go and Visual Studio Code installed, you have all you need to start coding.

I run the following Visual Studio Code command from Git Bash to install the Go extension: you can do the same.

The Go extension is essential for a nice coding experience.

Initializing a Module

To start a new Go project with modules, you need to initialize your module with a unique name. This is done by running a specific command.

Replace the default module path with your desired one. For example, github.com/your-username-or-organization/my-new-go-project should be replaced with your actual module path.

A unique module path is crucial to avoid conflicts with other projects. It should follow the Go module naming conventions. If you plan to publish your project as an open-source library, consider using a valid public repository URL.

This command creates a go.mod file in your project directory. The go.mod file contains your module's name and dependencies, ensuring your project is versioned and reproducible.

See what others are reading: Golang Read Image File

Creating a Go Project

Credit: youtube.com, Setting up a Go Microservice project from scratch or how I usually get started with Go

To start a new Go project, create an empty folder.

Type go mod init in this folder to initialize the Go modules.

Go modules are used for our dependencies.

You can also use go-blueprint to initialize a Go project. To do this, install go-blueprint with the command and open the tool in your browser.

Fill in the necessary information for the project, such as projectName, database, framework, and some advanced options if used.

Here are the steps to initialize a Go project with go-blueprint:

  • Install go-blueprint
  • Open the go-blueprint tool in your browser
  • Fill in the project information
  • Copy and paste the command into the terminal and press enter
  • Run the command

Once you have initialized the project, you can open the main.go file in the cmd/api folder and add a line of code.

Project Structure and Syntax

In a GoLang init project, the basic project structure is created with a single command: `go mod init`. This command initializes a new module, creating a `go.mod` file that contains the module's metadata.

The `go.mod` file is used to manage the project's dependencies and versions. It lists the required packages and their versions, ensuring consistency across the project.

The `go.mod` file is automatically updated when you add or remove dependencies, making it easy to manage your project's dependencies.

Readers also liked: Golang Mod

Common Application Directories

Credit: youtube.com, Keep your project structure simple!

In a typical project, you'll find several common application directories that serve specific purposes.

The `node_modules` directory contains all the project's dependencies, which are installed using npm or yarn.

Each project has a `src` directory where all the source code is stored, including the main application file.

The `tests` directory holds all the test files, which are used to verify the functionality of the code.

The `public` directory is where static assets like images, stylesheets, and scripts are stored, making them accessible to the public.

The `config` directory contains configuration files that define project settings and environment variables.

The `dist` directory is where the compiled and bundled code is stored, ready for deployment.

Syntax

In Go, we define a function using the func keyword.

Functions can be defined in a way that's similar to other languages, but Go has its own unique syntax.

To define a struct in Go, you can follow the syntax shown in the example.

If this caught your attention, see: Golang vs Go

Credit: youtube.com, Syntax (Part 1)

Structs in Go are essentially the equivalent of classes in other languages.

In Go, variables, functions, and structs that start with an upper case letter are public, while those that start with a lowercase letter are package-private.

This means that if you want to make a variable, function, or struct accessible from outside the package, you need to start its name with an upper case letter.

Type definitions in Go come after the variable name, as seen in the struct example.

To import other packages in Go, you simply put an import statement at the top of the file.

Every Go program must have at least one main package.

Take a look at this: Start Next Js App

Concurrency in Go

Concurrency in Go is built with concurrency in mind, making it a great choice for building software that requires it.

Go is designed to handle concurrency efficiently, allowing developers to write concurrent code with ease. This is achieved through the use of Go routines, which are lightweight threads.

Go routines can be thought of as lightweight threads, making it easier for developers with C# and Java backgrounds to understand the concept.

Go is a great choice for building software that requires concurrency, and its design makes it well-suited for this type of development.

You might enjoy: Golang Go

Project Management

Credit: youtube.com, Learning Golang: Dependencies, Modules and How to manage Packages

Project management is a breeze with Go-blueprint. You can start by initializing a Go project with the "go-blueprint" tool.

To begin, install go-blueprint with the command provided in the instructions. Once installed, open the "go-blueprint" tool on your browser using the link given. You'll then be prompted to fill in the necessary project information, such as projectName, database, framework, and any advanced options you might need.

Here are the key project management steps to keep in mind:

  • Initialize the project with "go-blueprint"
  • Install go-blueprint with the provided command
  • Fill in project information in the "go-blueprint" tool
  • Copy and run the command to set up the project

By following these steps, you'll be well on your way to successfully managing your Go project.

Managing Dependencies

Managing dependencies is a crucial part of any project. Go modules make it easy to manage dependencies, automatically downloading and caching required dependencies when you import a package in your code.

To add a new dependency to your project, use the go get command. This command downloads the package and adds it to your go.mod file as a dependency.

Readers also liked: Golang Args

Credit: youtube.com, How to Manage Project Dependencies

For example, to add the gin-gonic package to your project, you can run the command "go get gin-gonic/gin". This command ensures that your dependencies are properly set up and up-to-date.

After adding a new dependency, run the command "go mod tidy" to ensure that your go.mod and go.sum files are up to date. This command adds missing requirements and removes any unnecessary dependencies.

Expand your knowledge: Golang Mod Tidy

Test

To add tests in Go, you simply add a file named main_test.go, where you can add a function to test your methods, with a signature that MUST always be TestXxx(t *testing.T), and the First X must always be uppercase.

The first step is to create a test file, typically named main_test.go, where you can add your test function.

To run all tests in your project, including those in subfolders, use the ./... command in your terminal.

You can run tests with code coverage by using the same command with additional flags.

To see how tests fail, you can intentionally change the implementation of your code and run the tests again.

Project Compilation and Execution

Credit: youtube.com, Learn GO Fast: Full Tutorial

Compiling and running your Go project is a straightforward process. You can build an executable for your software by using the go build command.

To get started, you can simply invoke the executable and see the same result as you had with go run main.go. For more verbose output, use the -x option on go build.

You can compile the same code for Linux or your Raspberry Pi from your Windows machine. To run the binary on Linux, don't forget to chmod +x and then simply invoke using ./hello-world.

A Raspberry Pi 3 has an ARM v7 architecture, which can be compiled like this: go build -o hello-world main.go. To run the binary, do the same as on any other Linux machine: chmod +x and then ./hello-world.

Here's a quick rundown of the steps to compile and run your Go project on different platforms:

Now that you've compiled and run your Go project, it's time to initialize a new project with go-blueprint.

Viola Morissette

Assigning Editor

Viola Morissette is a seasoned Assigning Editor with a passion for curating high-quality content. With a keen eye for detail and a knack for identifying emerging trends, she has successfully guided numerous articles to publication. Her expertise spans a wide range of topics, including technology and software tutorials, such as her work on "OneDrive Tutorials," where she expertly assigned and edited pieces that have resonated with readers worldwide.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.