11.8 C
New York
Tuesday, October 4, 2022

Understanding a Minimal Go Program – MUO – MakeUseOf

It’s an enticing language, but the structure of a Go project is a little more involved than you might expect. Learn how to get set up with Go today.
Go's popularity has increased over the years. More companies are adopting Go, and the developer pool is growing as more use cases for the language roll out. People use it to build programs ranging from fast web apps, blockchain solutions, and machine learning tools.
Developers love Go because of its expressivity, ease of use, and high language performance. Go has a faster learning curve than most languages with a fast-growing ecosystem of packages and libraries.
The Go programming language isn't pre-installed on Windows, macOS, or most Linux distros. You'll need to install Go to run Go programs. You can check the official Go downloads page to install it on your machine. You'll want to install a recent version of Go to get the most from the language since newer versions have more features and updates.
Once you have Go installed, you can work with the language, execute Go files, create workspaces, and build projects in Go.
You can create a Go file and run Go programs using the standard library. However, if you want to use external libraries, you'll need to create a Go modules file for dependency management, version tracking, and more.
In Go, a module is a collection of packages in a file tree with a go.mod file at the root. That file defines the module's path, the import path, and the dependency requirements for a successful build process.
You can create a Go modules file with the Go mod command and the init subcommand before specifying the path or directory for the project.
The command creates the go.mod file. The argument after the init command is the module path. The module path can be the file path on your host environment, or the repository domain path.
When you install external packages and dependencies, go will update the require declaration in the go.mod file to ensure they’re included.
You can use the tidy subcommand of the mod command to download all dependencies required for your program.
The command will download all missing imports to the go modules file.
Every Go source file belongs to a package, and you can access code within a package namespace using its identifier.
You can have multiple namespaces for your packages. Once you create a folder, you've created a new namespace. You can access other namespaces with a dot (.) notation.
Here's an example of accessing a different namespace from another namespace.
You'll have to export the identifier by capitalizing the name to access an identifier in an external namespace.
The main function serves as the entry point for Go programs. You can't execute a Go file or package without the main function. You can have a main function in any namespace; however, you must have only one main function in a file or package.
Here's a simple Hello World Program to demonstrate the main function:
This code declares the main function in the main package namespace. It then imports the fmt package and uses the Println method to output a string to the console.
Compared to many other languages, importing packages and dependencies is easy. The import keyword provides functionality for importing packages. You can import packages from the standard library and external dependencies with the import keyword.
In the above example, you're importing one package. You can import multiple packages. You'll have to specify the packages in parentheses after the import statement.
Adding any delimiters in import statements is invalid. You can declare a custom name for imports by specifying the custom name before the package name.
Here, you imported the json package with the custom name as encoder. You'll have to access the package's functions and types with the custom name (encoder).
Some packages require you to import other packages for side effects. You'll have to prepend the package name with an underscore.
You can't access packages you've imported for side effects, but dependencies can if you configure them.
You'll use the run and build commands to compile and execute your Go code. They have similar functionalities, and you'll use them for executing packages.
The run command is a combination of compilation and execution instructions. The run command executes the package without creating any executables in the working directory. You'll have to specify the file name of the package name after the run command.
The build command is a compilation command that compiles a package or file into a binary executable.
If you run the build command without any arguments after the file or package name, go will generate an executable in your package’s root directory.
You'll have to recompile the program with the build command when you change a package.
You can specify a directory as an argument, and the build command will output the executable in the specified directory.
The Go standard library is powerful and intuitive. You can quickly build modern applications without having to install any external dependencies.
Since Go's release in 2009, developers and companies have used it for various use cases, in a range of fields. Its success is primarily because Go provides a Python-like syntax alongside C-like performance.
Goodness is a mechanical engineering student and software developer passionate about cloud technologies and the Go programming language.
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!

source

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles