How do you Setting up Go Programming Development Environment? Go is a very popular programming language, and it has become very popular for web applications. However, it has some drawbacks, such as not having a lot of libraries.
Setting Up Go Development Environment: Go is a language developed at Google that provides simplicity, speed, and efficiency
The Go programming language has been in development since 2000 and has been adopted by many companies such as Walmart, Mozilla, and Facebook. If you have worked with other languages such as C, Java, C++, Python, Ruby, or JavaScript, you will feel right at home using Go. Go is a general-purpose language that offers many features of object-oriented programming languages. However, it is not a full-blown object-oriented language. Go is a compiled language which means the code is converted to machine code before being executed. This ensures that the programs run faster and is very helpful when debugging. This article will show you how to install and configure Go on a Mac.
Steps To Install Go:
First, you will need to download the Go language from http://golang.org/. Go has a binary available for macOS and Linux, Windows and Android. You can use your favorite package manager to install it. I will be using Homebrew to install Go.
Next, go to the https://golang.org/dl page and click the “Download” link. A file named go1.13.tar.gz will be downloaded to your computer.
You will also need to download a package called golang. This is the package manager used for Go.
To install, simply double-click on the go1.13.tar.gz file.
It will begin installing the package and downloading the required dependencies. After the installation is completed, you should have a folder named go in your Applications directory.
You will now need to install the go toolchain. The toolchain is used to compile, build, and test Go programs. You can install the toolchain using the command:
sudo /usr/bin/go env
This will open a dialog box and the toolchain should be listed undergo. Click on the install button.
This will begin the installation process. You will now need to update the go environment. For this, simply open Terminal and enter the following commands:
cd ~
wget https://dl.google.com/go/go1.12.linux-amd64.tar.gz
tar -zxvf go1.12.linux-amd64.tar.gz
cd go
./configure
make
sudo make install
After the installation is completed, you should have a folder named bin in your Applications directory. This is the directory where the Go executable files are stored.
Now that you have installed Go, you will need to create a new Go project. Go has a command line interface to create a new project called to go. This is a simple way to create a new Go project. The syntax is going new.
This will open a dialog box where you can give your project a name. Type “hello-world” as the project name.
After creating the project, you will need to set the current working directory to the project folder. To do this, enter the following command:
cd ~
cd hello-world
Now that you have created the project, you will need to initialize the project. Go uses a simple text file called to go.mod to initialize the project. The go. mod file contains the go. mod.lock file which keeps track of the changes made to the go.mod file. The go.mod file must be edited manually before the first compilation is attempted. To edit the go.mod file, type the following command:
vi go.mod
Next, you will add the import statements for the packages that are required by the project. These are usually standard library packages. For example, the code for the HTTP server requires the following import statement:
import "net/http"
You will also need to add the import statements for the packages that are specific to the project. In the example project, the project uses the fmt package to print the date and time in the format yyyy-MM-dd HH:mm:ss. The fmt package comes pre-installed with the Go language. To add the import statement for the fmt package, type the following command:
import “fmt”
import "fmt"
The next step is to add the main function. The main function is the entry point of the application. It will be used to start the program once it is run.
When you type the go run hello-world.go, the compiler will look for the main function in the hello-world.go file.
For the hello world program to run, the main function must have an executable command and arguments. This can be done by adding the following lines to the go.mod file:
main = "Hello, world!"
You will now need to compile the project. The go tool will look for the go.mod file in the current working directory. It will check if there are any changes made in the go.mod file. If there are no changes in the go.mod file, then the go tool will use the go.mod.lock file to know that the go.mod file doesn’t need to be changed.
There are many ways to set up your development environment. In this article, I’m going to show you how to set up your development environment using the official Go tools.
First, let’s see what dependencies we need.
1. A working Go environment.
You can get it from any Go website, such as golang.org.
2. Git.
3. Build tool.
4. An editor.
5. A terminal.
I personally prefer the command line, so I’ll be using it in this article.
Go 1.8.3
git
gofmt
godep
goinstall
vim
tmux
sudo apt install git golang-tools-eol
Next, we can go ahead and create a new directory, and make it the root directory of our project.
mkdir my-first-project && cd my-first-project
Create a package.json file to define the structure of your project.
nano package.json
Now we can write the structure of our project.
{
"name": "my-first-project",
"version": "0.0.1",
"description": "My first project",
"author": "Me",
"license": "MIT",
"dependencies": {
"github": "https://github.com//"
}
}
Now, we can install all of the dependencies that we defined in package.json.
npm i github@0.0.1 -g
The -g flag means that npm will install all of the packages globally. If you don’t want to install globally, then you can simply use npm install.
npm install -g github@0.0.1
To verify that everything went well, let’s check the version of the dependencies.
npm -v
7.1.0
Now, we can run our application.
node index.js
Let’s verify that our application works.
http://localhost:8080
We can now move our project into a real folder and publish it.
mkdir my-first-project-in-real-folder && cd my-first-project-in-real-folder
npm link
ln -s /path/to/my-first-project/.
If you are using tmux, it is recommended to create a new session and start it.
tmux new-session -d -s my-first-project
cd my-first-project
Finally, we can verify that our application works.
http://localhost:8080
Here is the summary of what we did.
1. Created a new directory and moved our project into it.
2. Set up a Go environment.
3. Created a package.json file.
4. Installed all the dependencies that we need.
5. Run the application.
6. Verified that our application works.
Now, you are ready to write Go programs. The official documentation of Go has listed some of the most common ways of using the Go language.
- Go is an imperative programming language. In this context, imperative means that instructions in the program control the flow of the program. The language is also concurrent in nature. In concurrent programming, more than one thread executes the same program.
- Go is a compiled language. This means that the language constructs are translated to machine code. This reduces the overhead of the code. The compiler also generates the code in a standard manner, which makes the code more readable and maintainable.
- Go is statically typed. This means that the language does not support dynamic typing. In static typing, the language does not permit variables to be assigned a value at runtime. It checks the value of the variable at runtime.
- Go is garbage collected. This means that the language supports automatic memory management. The garbage collector automatically frees unused memory, which makes the memory more efficient.
- Go is strongly typed. This means that the language supports type checking. It enforces the type of data being passed.
- The official website of Go lists the following features of the language:
- Go is a compiled language. In this context, compilation means that the source code is transformed into machine language.
- Go is a concurrent programming language. In this context, concurrency means that the language allows more than one thread to execute the same program.
- Go is a statically typed language. In this context, the language does not allow runtime type checking.
- Go is a statically typed language. In this context, the type of data being used is checked at compile time.
- Go is a garbage-collected language. In this context, the language supports automatic memory management.
- Go is a strongly typed language. In this context, the language enforces the type of data being used.
- Go is a language built for creating programs that are easy to debug and hard to crash.
- Go is a language for building large systems.
- Go is a language for building complex applications.
- Go is a language for writing concurrent applications.
- Go is a language that focuses on the simplicity of the language.
- Go is a language that focuses on the security of the language.