Learning Go Lang: A Beginner’s Perspective

Barefoot Techie
thedailypersistence
8 min readApr 2, 2023
Photo by Marcus Dall Col on Unsplash

Go Lang, or Golang, is a modern, open-source programming language that has gained a lot of popularity in recent years. I was looking for a language that would be easy to pick up just over the weekend, with a wide range of applications, and a good balance of simplicity and power. After trying out several languages, I came across Go, and I was immediately impressed with its simplicity, efficiency, and versatility.

In this article, I will share my experience of learning Go Lang as a beginner, and how easy it is to pick up its advantages. I will also provide an example of building a simple program and a relatively complex one to illustrate the language’s capabilities. Additionally, I will discuss what can be built using Go and its limitations.

Advantages of Go Lang

The following are some of the advantages of Go Lang that make it an ideal choice for beginners:

Simplicity: Go Lang has a simple syntax that is easy to learn and understand. It has a concise and consistent grammar that makes the code more readable and easier to write. This simplicity makes it easier to understand what the code does, even for beginners.

Efficiency: Go Lang is known for its fast compile and execution times, which makes it suitable for building high-performance applications. It has a garbage collector that manages memory efficiently, freeing up the programmer from manual memory management.

Concurrency: Go Lang has built-in support for concurrency, making it easy to write parallel programs that can take advantage of multi-core processors. This feature is critical for building scalable systems that can handle high traffic.

Versatility: Go Lang is a general-purpose language that can be used for a wide range of applications, from system programming to web development. It has a standard library that provides a comprehensive set of tools for building different types of applications.

Example of Building a Simple Program

To illustrate how easy it is to pick up Go Lang, let’s build a simple program that prints out the “Hello, World!” message to the console.

package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}

In this program, we first declare the package name as “main”. This is the package that Go Lang uses to build executable programs. We then import the “fmt” package, which provides functions for formatting text.

We then define the “main” function, which is the entry point of our program. In the main function, we use the “fmt.Println” function to print out the “Hello, World!” message to the console. Finally, we run the program using the “go run” command.

Example of Building a Relatively Complex Program

Let’s now look at a relatively complex program to demonstrate the capabilities of Go Lang. In this program, we will build a simple web server that serves static files.

package main

import (
"net/http"
)

func main() {
// Serve static files from the "public" directory
fs := http.FileServer(http.Dir("public"))
http.Handle("/", fs)

// Start the server on port 8080
http.ListenAndServe(":8080", nil)
}

In this program, we first import the “net/http” package, which provides functions for building HTTP servers and clients. We then define the “main” function, which is the entry point of our program.

In the main function, we create a file server that serves static files from the “public” directory using the “http.FileServer” function. We then use the “http.Handle” function to handle all requests to the root URL (“/”) and serve the files from the file server.

Finally, we start the server on port 8080 using the “http.ListenAndServe” function.

This program demonstrates the ease of building a simple web server in Go Lang. We can extend this program to handle more complex requests, such as dynamic content and API endpoints.

What Can Be Built Using Go Lang?

Go Lang can be used for a wide range of applications, including:

Web development: Go Lang has built-in support for building web applications. The standard library provides a comprehensive set of tools for building web servers, handling HTTP requests and responses, and serving static and dynamic content.

System programming: Go Lang is suitable for building low-level systems programming tasks, such as writing device drivers, network protocols, and operating systems.

Network programming: Go Lang has built-in support for network programming, making it easy to write applications that communicate over the network.

Big data: Go Lang is suitable for building applications that process large amounts of data. It has built-in support for concurrency and parallelism, which makes it easy to write programs that can take advantage of multi-core processors.

Machine learning: Go Lang has several machine learning libraries, such as GoLearn and Gorgonia, that make it suitable for building machine learning applications.

Limitations of Go Lang

While Go Lang has many advantages, it also has some limitations:

Lack of Generics: Go Lang does not have support for generic types, which can make it challenging to write reusable code for generic data types.

Immature Ecosystem: While Go Lang has a growing community, its ecosystem is not as mature as other languages like Python and Java. This means that some libraries and tools may not be available, and documentation may not be as extensive.

No support for GUI: Go Lang does not have built-in support for developing graphical user interfaces (GUIs). This means that building desktop applications or mobile apps with GUIs may require additional libraries or tools.

I found Go Lang to be an ideal language for learning. Its simplicity, efficiency, and versatility make it an excellent choice for building a wide range of applications. With its growing community and support, Go Lang is becoming an increasingly popular choice for developers. While it has some limitations, its advantages make it a language worth exploring.

Whether you are building a small command-line tool or a complex web application, Go Lang has the tools and libraries to help you get the job done. The language’s simplicity and efficiency make it easy to learn, and its built-in concurrency support allows developers to take advantage of modern computing architectures.

As we have seen in the examples above, building a simple program in Go Lang is straightforward, and we can quickly extend it to handle more complex tasks. Go Lang’s standard library has a comprehensive set of tools for handling HTTP requests and responses, making it easy to build web servers and APIs.

In addition to its built-in features, Go Lang has an active community that contributes to a growing ecosystem of libraries and tools. While the language is still relatively new compared to more established languages, it has gained traction in recent years, making it a language worth considering for your next project.

Overall, Go Lang is a powerful and efficient language that offers many advantages for developers. Its simplicity, versatility, and growing ecosystem make it an excellent choice for building a wide range of applications. As a beginner in Go Lang, I look forward to exploring its capabilities and building more complex programs in the future.

To further illustrate the capabilities of Go Lang, let us consider a more complex program. Let’s say we want to build a service that retrieves weather data from an external API and returns the data in JSON format.

To accomplish this, we will need to make a request to the external API, parse the response, and format the data in JSON. Here’s how we can do it in Go Lang:

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

type WeatherData struct {
Temperature float64 `json:"temperature"`
WindSpeed float64 `json:"wind_speed"`
}

func main() {
http.HandleFunc("/weather", handleWeatherRequest)
http.ListenAndServe(":8080", nil)
}

func handleWeatherRequest(w http.ResponseWriter, r *http.Request) {
url := "https://api.openweathermap.org/data/2.5/weather?q=New+York&appid=YOUR_API_KEY"
resp, err := http.Get(url)
if err != nil {
http.Error(w, "Error retrieving weather data", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Error reading weather data", http.StatusInternalServerError)
return
}

var data struct {
Main struct {
Temp float64 `json:"temp"`
} `json:"main"`
Wind struct {
Speed float64 `json:"speed"`
} `json:"wind"`
}

err = json.Unmarshal(body, &data)
if err != nil {
http.Error(w, "Error parsing weather data", http.StatusInternalServerError)
return
}

weatherData := WeatherData{
Temperature: data.Main.Temp,
WindSpeed: data.Wind.Speed,
}

jsonData, err := json.Marshal(weatherData)
if err != nil {
http.Error(w, "Error formatting weather data", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(jsonData))
}

In this program, we define a struct WeatherData that represents the weather data we want to return. We then define a function handleWeatherRequest that handles the request to the “/weather” endpoint.

In the function, we make a request to the OpenWeatherMap API to retrieve weather data for New York. We then parse the response and extract the temperature and wind speed values, which we use to create an instance of the WeatherData struct. We then format the data as JSON and return it to the client.

This program demonstrates the power of Go Lang for building web services that interact with external APIs. With Go Lang’s support for concurrency and built-in tools for handling HTTP requests and responses, it is easy to build efficient and scalable web services.

Limitations of Go Lang

While Go Lang has many advantages, it also has some limitations:

Lack of Generics: Go Lang does not have support for generic types, which can make it challenging to write reusable code for generic data types.

Immature Ecosystem: While Go Lang has a growing community, its ecosystem is not as mature as other languages like Python and Java. This means that some libraries and tools may not be available, and documentation may not be as extensive.

No support for GUI: Go Lang does not have built-in support for developing graphical user interfaces (GUIs). This means that building desktop applications or mobile apps with GUIs may require additional libraries or tools.

Conclusion

Go Lang is a powerful and versatile language that offers many advantages for developers. Its simplicity, efficiency, and built-in concurrency support make it easy to build a wide range of applications, from small command-line tools to complex web services.

Go Lang’s standard library provides a rich set of tools for handling HTTP requests and responses, making it easy to build web servers and APIs. Additionally, the language’s support for concurrency allows developers to take advantage of modern computing architectures and build highly scalable systems.

Furthermore, Go Lang has an active community that contributes to a growing ecosystem of libraries and tools. While the language is still relatively new compared to more established languages, it has gained traction in recent years, making it a language worth considering for your next project.

However, Go Lang also has some limitations, such as lack of support for generics and GUIs, and an immature ecosystem compared to more established languages. These limitations may make it challenging for developers who are used to working with other languages or who require specific functionality that is not available in Go Lang.

Overall, Go Lang is an excellent language for building efficient, scalable, and reliable applications. Its simplicity and efficiency make it easy to learn, and its built-in concurrency support makes it an excellent choice for building high-performance systems. As a developer who has recently picked up Go Lang, I can attest to its ease of use and look forward to exploring its capabilities further.

--

--