Go Programming Language

Okan Sungur
5 min readJun 25, 2022

--

GoLang is not a new technology and it doesn’t offer new features. It is not object-oriented, no inheritance, no method overloading, and no exception handling…
What makes go a good choice is; that it is best suited for system programming. Also when writing light-weighted microservice architecture, go is widely preferred for backend structure.

GO Logo

Go allows parallel processing and in this way, we can use thousands of parallel threads. As a developer, we don’t have to worry about creating them or making them communicate with each other. This allows us to write high-performance applications. Go is a fast compiled language because the final output is a stand-alone executable and it doesn’t need external dependencies.
It is a simple and clear language concerning other programming languages. For new programmers, it may be difficult to understand someone else’s code but go makes it easy. The go software can be reassembled to run on Mac OS, Linux, Windows, and smartphones.

Goroutine is a lightweight thread of execution. Using threads is easier when compared with java. The default stack size depends on the architecture and your operating system. But Goroutine’s minimum stack size is defined as 2048 bytes (since Go 1.4) and it can grow and shrink as needed.

Kubernetes is written in the Go language. And it is inspired by Borg (a system used by Google for over 15 years, to manage applications). We can create plugins for Kubernetes by using Go.

If you want to start learning GoLang, you don’t have to install anything on your computer; instead, you can just investigate the go playground.

If you want to create a sample application just follow the steps. We will:

  • Create a server with go serve some static HTML files
  • Connect to PostgreSQL with Docker
  • Create a web form post some data and check the results

But before we have to complete the installation steps

From Visual Studio Code select the plugins menu and add the Go plugin and then from the view menu select command palette type Go install update option. This will install all available packages necessary.

The complete source code is available on GitHub. To run our sample first, ensure that Docker is installed on your machine. And run the command “docker compose up”.

Docker Container golang

If you create a new project by copying only ‘main. go’, please execute the following commands to create the mod file and download the necessary packages

Student List
Static Context served by Go
Form Page to Insert Data by Using Go
The Student List After Adding Data

Now let’s take a look at our “main. go” file.

main.go

Package main tells the Go compiler that the package should be compiled as an executable program. The main function is the entry point. When we type “/student” from the address bar we call the getStudentHandler() method with the ‘Get’ request. In Go nil is a zero value. Student struct is a collection of fields that are used for grouping data together, to form records.

Gorilla is a web framework. It has a routing package that we will use in our example. “gorilla/mux” package implements a request router and dispatcher for matching incoming requests to their respective handler. “mux” stands for “HTTP request multiplexer”. After creating the router we have to register routes mapping URL paths to handlers. HTTP Stripprefix forwards the processing by modifying the request URL by stripping the specified prefix.

Struct is a collection of data fields with declared names and data types. If you are familiar with OOP we can think of struct-like classes. Go does not have an object-oriented architecture so we have structs that hold complex data structures. The name and type can not be changed at runtime.

The encoding/json package generates JSON documents from Go objects We have used the Marshal function to convert Go objects to JSON.

“strconv” package provides conversions to and from string representations of basic data types, ‘strconv. Itoa’ takes one parameter of int type and returns a string and strong. Atoi is used to convert string type into the integer type.

YAML support for the Go language is provided by “gopkg.in/yaml.v3” package. We used studentRouter to create a student struct for YAML. The marshal method is to serialize the values into a YAML document.

We used “database/sql”, a package containing a common interface for sql-related operations. To use it we need a concrete implementation of the interface, and it’s the database driver. If _ “github.com/lib/pq” package is forgotten we will get the error “Error sql: unknown driver “postgres” (forgotten import?)”

Rows are the resultset, “db. Query” for querying database To avoid SQL injection do not use string formatting functions such as fmt.

Sprintf query := fmt.Sprintf(“SELECT * FROM school WHERE name = XX” );

We read the columns in each row into variables with rows.Scan() We check for errors after we’re done iterating over the rows.

FormValue parameter we get the parameters from the inputs provided by the Html page with the getError() method we print the error message as a response.

If you are using the docker file or docker-compose and get the exception “dial tcp 127.0.0.1:5432: connect: connection refused” change the host as “host.docker.internal”. This will fix the error.

Panic() is an inbuilt function in go. We use it for handling particular errors and it is also by the program itself when an unexpected error occurs at runtime.

Thanks for reading.

--

--