Go or Golang, is one of the most advanced programming languages developed by Google. It is a great language renowned for its speed, efficiency, and simplicity. Go can help identify the potential setbacks of developing concurrent and scalable systems.
The best part is that Go offers powerful tools and simple syntax to streamline the development of microservices, web apps, and cloud-based solutions. Today’s guide will explore the step-by-step guide on getting started with Go to help you make the most of it like a pro.
Let’s Get Started!
Key Necessities to Consider:
Some key conditions are listed below for better understanding:
• Prior Programming Experience: Though the coding is simple, ensure you have prior knowledge to enhance your experience with functions.
• Code Editing Tool: You’ll need a text editor to edit your code. You can go for VSCode, a free tool, or GoLand, a paid one.
• A Command Terminal: Enhance the go functionality using any terminal on Mac and Linux, and on PowerShell in Windows.
Install Go:
To install Go, you will need to visit the official Go website and download the installer for your system. Ensure you follow the installation instructions and verify by running Go version in your terminal to confirm Go is correctly installed.
Write Some Simple Code:
You can begin with Hello, World.
1. You can open a command prompt and switch to your home directory using the cd command.
On Linux or Mac:
cd
On Windows:
cd %HOMEPATH%
2. Next, you will need to set up a hello directory for your first Go source code.
For instance, follow these commands:
Mkdir hello
cd hello
3. Enable Dependency Tracking For Code
When your code relies on packages from other modules, their dependencies are managed through your module. This module is defined by a go.mod file, which records the modules providing those packages. The go.mod file remains with your code, even in your source repository.
You will need to create a go.mod file and run the go mod init command to enhance dependency tracking for your code. Ensure you give it the name of the module your code will be in. The name is the module’s module path.
In real-world development, the module path usually corresponds to the repository location where your source code is stored. For instance, it might be github.com/mymodule. If you intend to share your module with others, the module path must point to a location accessible for downloading via Go tools. For detailed guidance on naming a module and setting its module path, refer to the documentation on managing dependencies.
Here is the following command
$ go mod init example/hello
go: creating a new go.mod: module example/hello
4. Here comes an opportunity to use your text editor tool. Use it to create a file hello.go and write your code.
5. All you need to do is to paste the following code into your hello.go file and ensure you save it.
package main
import “fmt”
func main() {
fmt.Println(“Hello, World!”)
}
Here is your Go Code. Now, you can do the following in the code:
• Declare a main package (it is a process to group multiple functions and is created with all files in the same directory).
• Importing the popular fmt package, containing functions for formatting text. This includes printing to the console. It is one of the standard library packages you get when you install Go
• Add a main function to print a message to the console. It will be implemented by default as you run the main package.
6. Run the code to overview the greeting,
$ go run,
Hello, World!
The go run command is the best command to implement things with Go. You can use the following to access a list of the others:
$ go help
Call code in an external package
You will need a package that has functions you can use in your code if you want your code to do something that has been used by someone else.
1. Create your printed message a little more interesting with a function from an external module.
- Next, access pkg.go.dev and search for a “quote” package.
- Find and click the rsc.io/quote package in the search results.
- Index -> Documentation -> Note the list of functions you can call from your code. You can use the Go function
- At the page top, note that the package quote is added in the rsc.io/quote module.
The best part is that you can easily use the pkg.go.dev site to locate published modules whose packages have similar functions that you can use in your code.
2. In your Go code, you will need to import the rsc.io/quote package and add a call to its Go function.
package main
import “fmt”
import “rsc.io/quote”
func main() {
fmt.Println(quote.Go())
}
3. Include new module requirements and sums
Go will include the quote module as a requirement and a go.sum file for use in confirming the module. You can also see other authenticating modules in the Go Module Reference:
$ go mod tidy
go: finding a module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2
4. Execute your code to view the output produced by the function you invoked.
$ go run .
Don’t communicate by sharing memory, share memory by communicating.
Note: Your code calls the Go function, Printing a clever message about communication.
Notice that your code calls the Go function, printing a clever message about communication.
When you executed go mod tidy, it identified and downloaded the rsc.io/quote module, which includes the package you imported. By default, it retrieved the latest available version, v1.5.2.
Show Your Creativity
This easy-to-understand guide will help you get started with the Go programming language in a breeze. Install, write your code, and show your creativity.