DEV Community

Alec V
Alec V

Posted on • Edited on

Go Language Basics

Go Gopher

Go is an open source programming language developed by software engineers at Google. It is also known as Golang because of its first web address, but its official name is Go. Go was designed by Ken Thompson, one of the creators of the C programming language and Unix, Rob Pike, one of the creators of UTF-8 and unix, and Robert Greismer. Their goal was to create a language that had a runtime efficiency similar to C, but was as readable as Javascript and Python.

Go is a fast growing language. It was created in 2007, released as an open-source project in 2009, and the first version was released in 2012. Githut 2.0 ranked it as third most popular language on Github by pull request for the first and second quarter of 2023, behind Python and Java. Go was recognized by TIOBE as language of the year in 2009 and 2016 and as of September of 2023, it ranked 12th on the TIOBE index.

Githut Popularity

Source: Githut 2.0

How does it compare to other languages? Go uses static typing, which means that every variable’s data type must be assigned when the code is compiled. Static typing can make code more robust and reliable. Go is garbage collected, it automatically attempts to reclaim memory that is no longer in use by the program, like C# and Java, which also makes it more memory efficient and easier to use. Go is compiled directly to machine code, unlike other similar languages like Python and Java which are compiled to byte code and then run on a virtual machine. This makes Go more memory efficient than those languages. Go is a very minimalist language, it has only 25 keywords.

Companies using Go
Companies using Go

Source: go.dev

Go is an object oriented language, but does not support class inheritance. Instead, Go uses interfaces and structs which are similar to classes. Go must be compiled to run, but it compiles much more quickly than other languages like C, C++, Java, and Rust. Andrew Gerrand said in an interview with Jeff Meyerson that "there's a joke that Go was conceived while waiting for a C++ program to compile, which is kind of half true" (Meyerson, 101).

Where Go really excels is in its efficient model for running concurrent functions. Go does not use threads for concurrency, but its own model called goroutines. These goroutines have a flexible stack that grows as necessary and starts out using only 2KB of memory, which makes goroutines much more memory efficient than threads, so many more goroutines can be created than threads. Goroutines communicate with one another through bidirectional channels.

Go’s syntax is similar to C and Java because it uses curly braces to enclose blocks of code. To print the traditional “Hello, world!” function to the console, you need to declare the package that the file belongs to, import a library that will allow you to print things to console, declare a function and finally put the Println command in the function. It looks like:

package main

import "fmt"

func main (){
    fmt.Println("Hello, world!")
}
Enter fullscreen mode Exit fullscreen mode

Datatypes in go are divided into basic, aggregate, reference, and interface. Basic data includes strings, numbers, and Booleans. Aggregate data includes arrays and structs. Reference data includes pointers, slices, functions, and channels. Interfaces allow a developer to implement polymorphism and code flexibility without using inheritance.

Variables can be declared in a variety of ways and their datatype can be specified explicitly or be inferred. For example:


package main

import "fmt"

func main (){
    var num0 int
    var num1 = 1
    num2 := 2
    var num3 int = 3
    num4, num5 := 4, 5
    arr := []int{num0, num1, num2, num3, num4, num5}

    var sum = 0

    for num := range arr{
        sum += num
    }

    fmt.Printf("%d\n", sum)
}
Enter fullscreen mode Exit fullscreen mode

Above I first declare a num0 variable without a value using the var keyword and int to specify the datatype. If a value is not specified for an integer, it will be initialized to 0. Next I declare num1 without specifying the datatype, but because its value is 1, it is inferred to be an integer. The := allows variable declaration and assignment without a keyword. Multiple variables can be declared at the same time, as with num4 and num5. Below I declare an array containing the above variables and a sum variable and then I use a for loop to iterate over the array and add up the values. Finally, I print the value.

Go is a young and fast-growing language that is in use at many large and successful businesses. It pairs speed and efficiency with readability and minimalist language design. This makes Go an easy language to learn and use that is very powerful, particularly in concurrent operations. Go has a large and growing library and is changing and growing rapidly. While it may require more lines of code to complete a project than some of its competitors, its power, clarity and ease of use suggests it will only grow in popularity.

Meyerson, J. (2014). The go programming language. IEEE Software, 31(5), 104–104. https://doi.org/10.1109/ms.2014.127
Lee, W.-M. (2021). Go programming language for dummies. John Wiley & Sons Inc.

Top comments (0)