
Setting up a local development environment for Go can be a breeze with the right tools. You can use Goland, a popular IDE, to write, run, and debug your Go code.
Goland provides features like code completion, debugging, and project navigation, making it a great choice for Go developers. It also supports Go modules and has a built-in terminal.
With Goland, you can create a new Go project and start coding right away. The IDE takes care of the setup for you, so you can focus on writing your code.
To run your Go code, you can use the built-in terminal in Goland to execute the `go run` command. This will compile and run your code in one step.
Intriguing read: Golang Source
Setting Up GoLang
To get started with GoLang, you'll want to set up the Go environment on your Mac. This involves installing the Go tools and creating a Go folder.
First, you'll need to install the Go tools. You can do this by running the command: $ echo "export GOPATH=$HOME/go" >> ~/.bash_profile && echo "export PATH=$PATH:$GOPATH/bin" >> ~/.bash_profile
For more insights, see: Go Html Template
This command will store the location of your Go programs and the location of your bin folder in your bash profile.
Next, navigate to your Go folder using the command: $ cd $HOME/go
Then, create a folder called src using the mkdir command: $ mkdir src
Now, create a folder called hello: $ mkdir hello
Navigate inside the hello folder using the command: $ cd hello
Make a file called hello.go using the touch command: $ touch hello.go
Open hello.go using your code editor and paste in the code provided in the installation page.
Here's a step-by-step guide to creating a Go project:
Managing Dependencies
You can configure Go.Mod to use local dependencies, making it easy to test patches to a library without committing changes.
This setup is excellent for creating and testing patches, as changes made on disk are reflected immediately in your project.
To use a concrete example, suppose your module depends on the popular package go-cmp, which lets you deep-compare arbitrary Go values.
Intriguing read: Golang Use Cases
You can download the dependency locally and patch it by cloning the repository into a local directory.
Edit the dependency's code to add a log statement, and Go has no idea you've cloned the dependency locally, so you'll need to add a replace line to your go.mod file.
gohack is a tool designed to address this use case, and it can fetch the dependency's code and store it somewhere locally.
Here are the steps to use gohack:
- Fetch the dependency's code and store it somewhere locally.
- Add a replace line to your go.mod file to point to the local directory.
Alternatively, you can use a module replace directive in your go.mod file to redirect uses of the dependency to a local directory.
The replace directive looks like this: `replace github.com/google/go-cmp => $DEP`.
Consider reading: Go vs Golang
Creating and Running Go Programs
To create and run Go programs, start by downloading Go from Golang's download page and select your operating system. You'll be redirected to the installation page.
You should download the installer as a .zip file, which you'll then open and follow the installation wizard instructions.
The installation steps vary depending on your operating system, so be sure to follow the instructions for Mac or Windows machines.
After installation, you'll be able to run Go programs in the terminal, where you should see "hello, world" printed out.
Suggestion: How to Run Golang Program
GoLang Packages and Imports
A package in GoLang is simply a directory that contains one or more Go source files or other Go packages. Every Go source file must belong to a package.
By using packages, you can reduce conflicts between function names, making them shorter and more concise. This is because the names of functions might be the same across many packages, but the package name helps to distinguish them.
Making your program organized is another benefit of using packages. It groups together pieces of code that are related, making it easier to locate the code you want to reuse.
To import local packages, you need to add lines to your main package file. This allows you to use the code in each local package.
An identifier is exported to permit access to it from another package if it meets two conditions: its first character is a Unicode uppercase letter, and it's declared in the package block or it's a field name or method name.
Noted that your workspace must be located inside the GOROOT or GOPATH folder, or else you'll encounter an error.
A fresh viewpoint: Golang Go
Main Function and Main Package
The main function and main package are crucial components of a GoLang application. The main function is the entry point for the application and must be declared in the main package.
The main package must have a package name of "main" and declare a function named "main" that takes no arguments and returns no value. Program execution begins by initializing the main package and then invoking the main function.
The main function invocation returns when the function returns, and the program exits without waiting for other goroutines to complete. This is important to note, especially when building shared libraries, as there will be no main package and main function in the package.
You can think of the main function and main package as the starting point of your GoLang application, where execution begins and ends.
Additional reading: Golang Programs
GoLang Import Packages
A package in GoLang is only a directory inside your workspace that contains one or more Go source files or other Go packages.
To import your local packages, you'll need to add the following lines to your main package's main.go file. This allows you to use the code in each of your local packages.
Here's an example of how to import local packages:
- In your main.go file, add the following lines: `import "pack1"` and `import "pack2"`
- Make sure your workspace is located inside the GOROOT or GOPATH folder, or else you'll get an error.
Note that an identifier may be exported to permit access to it from another package. An identifier is exported if both its first character is a Unicode uppercase letter and it's declared in the package block or it's a field name or method name.
In GoLang, you don't need to recompile the entire program when you make changes to a package. Just the smaller, modified portions of the program need to be recompiled.
Here's an interesting read: Install Golang Package
Go Development Tools
To get started with Go development, you'll need to download the Go installer from Golang's download page.
The installer will be downloaded as a .zip file, which you'll need to open and follow through with the installation wizard instructions.
You can choose to download the installer for your operating system, such as Mac or Windows.
After installation, you'll be able to run Go programs and see the "hello, world" output in the terminal, which means you're all set to start coding with Go.
Pkgsite Cmd
The pkgsite cmd is a powerful tool for setting up a local server version of pkg.go.dev. It hosts the cli pkg.go.dev uses for docs rendering.
You can install it using the command "go install" and it should just work. The recommendation to locally clone the repo, build it, and install it somewhere in your $PATH is not necessary.
Once installed, you can run it in the directory where your go package repo lives. This will render the documentation for your local packages, although you won't see any packages if you search because we're rendering just the local package.
You can customize the port by using the flag "-http localhost:3030". Opening up the localhost site will show you a page similar to pkg.go.dev with the search bar and everything.
A unique perspective: Golang Search
Using Go Workspaces
Go workspaces have been around since Go version 1.18, and they're a game-changer for working with multi-module repositories and large monorepos.
Using a workspace makes it easier to implement certain use cases, and it's also a cleaner way to work with temporary patching. You can remove replace directives from your go.mod file and still achieve the desired results.
To get started, you'll need to run a few commands in your test module's directory. First, you'll need to initialize an empty workspace, which will create a new file called go.work.
This file will contain use directives that include the current directory and the local version of the dependency you've checked out. Don't worry, your go.mod file won't be modified.
By using a go.work file, you're creating a convenient local development environment that's separate from your module's actual source code. This approach is safer than leaving behind replace directives in your go.mod file, which can cause issues when testing.
Here's a quick rundown of the steps to follow:
- Initialize an empty workspace in the current directory.
- Add use directives to go.work for including the current directory and the local version of the dependency.
By following these steps, you'll be able to work with temporary patching in a cleaner and more efficient way. And with Go workspaces, you'll be able to take advantage of the benefits they offer, even with large monorepos.
Using Gohack
Using gohack is a great way to address a specific use case in Go development.
gohack is a tool designed to fetch a dependency's code and store it locally, allowing you to control where these are stored by setting the $GOHACK env var.
To install gohack, you'll need to follow the installation instructions.
You can then run the gohack command to fetch the dependency's code and add a replace line to your go.mod file.
Here's what the gohack command does in detail:
- Fetch the dependency's code and store it somewhere locally.
- Add a replace line to your go.mod file to point to the new location.
Since gohack places the dependency in a new location, you'll need to edit the cmp/compare.go file to add a log statement.
You can easily undo changes with the gohack undo command.
Choosing an Approach
You can use gohack for a quick check, as it obtains the dependency on its own, making it faster than cloning manually.
However, committing the replace line accidentally is a concern, which is why the workspace approach is safer and more explicit.
gohack's speed comes at the cost of potential mistakes, so it's essential to weigh the tradeoffs.
gohack is not the only option, as go mod vendor is another way to accomplish this, but you should read the documentation to learn more about its tradeoffs.
Featured Images: pexels.com


