Sunteți pe pagina 1din 9

Go Language (Golang)

Introduction
Developed by Robert Griesemer, Rob Pike, and Ken Thompson at Google in 2007, Go
language or often referred to as “Golang” is a procedural programming language and was
launched as an open-source programming language in 2009 and version 1.0 was released in
March 2012. When creating the language, the creators at the time looked to the 3 major
languages in use in Google: Python, C/C++, and Java. While creating Golang, the creators were
running into limitations in the previous 3 major languages that might not be able to be given the
history and the design of said languages. All three of the said languages were created in a time
where multithreaded languages were rare as the compilers just needed to read the code one
line at a time, so concurrency in these languages are patched in at best. In Go, procedures
(functions) are stitched together using packages to form a program. It is assembled this way to
allow for efficient management of dependencies. Go is syntactically almost like C, but with
memory safety, garbage pickup , structural typing, and CSP-style concurrency. Environment
adopting patterns are supported by this language which are likened to dynamic languages.

The book entitled, An Introduction to Web Development in Go, Go is described as “a


procedural, imperative, modular, object-oriented, statically typed, garbage-collected, compiled
programming language with syntax broadly similar to the C family of languages (C, C++, C#,
Java, Javascript, etc…) and CSP-inspired concurrency features“.

Go is a procedural programming language because functions are stitched together to


form a program and set commands are followed in order. It is an imperative programming
language because functions are built out of statements, which are commands that will then be
performed by the computer.

Many companies use Go, to name a few, Google, Uber, Twitch, Dailymotion, and
Dropbox. Go works best for developing applications that are for networking, distributed
functions, and cloud services. It is also well-suited for building cloud-native apps and for
replacing existing infrastructures. Utilities and stand-alone tools are ideal to develop using Go.

Go shines when used for distributed networked services. Using Go’s features such as
goroutines, many Go projects can be developed for networking, distributed functions and cloud
services: APIs, web servers, minimal frameworks for web applications along with others.
Building cloud-native apps is befitting for Go because of its concurrency and networking
features, and because of its portability. Docker, Kubernetes, and Istio are examples where Go
was used for cloud-native computing. Go is also being used to replace existing infrastructure,
which serves many advantages like better memory safety, easier cross-platform deployment,
and a clean code base for maintenance. Teleport, a new SSH server and a new version of
Network Time Protocol are currently being written in Go as an offered replacement. Go is ideal
for creating utilities and other tooling since they can be launched quickly, can be readily
packaged up for redistribution, and compile to binaries with minimal external dependencies.
Readability
Go code is articulate, concise and provides a simple API. It is readable, top-down code and has
precise comments. The readability of go language is like python and java script although
developers do not encourage you to write like python, java, or javascript.

Writability
It is strict about how you write your code which means once you write something, it is
understood by everyone. It follows the simplicity of "C" in syntax and semantics with some
additional features.

Reliability
Go does not have exceptions, instead Go allows a function to return an error type as well as a
result. Whenever a function returns an error, inspecting the error is necessary in order to know
anything about the value. Go also has a facility named panic, it is used in exceptional
circumstances where there is no possibility for a code, or anyone integrating the code to
proceed.

Cost
Go is an open source programming language that makes it easier to build a simple, reliable, and
efficient software. Go language is free and it is an open source which means that anyone has
access to it. The training cost depends if the trainer or tutorial requires a payment but the
language is easier to learn if you have a background in python and java. There are resources
online if you want to learn it. Overall, the cost of this language is not much because it's an open
source but it depends on the training fee or the tutorial fee. The Go language has concurrency
built into the system which creates faster compile times versus other languages. Golang also
has a rich web-oriented runtime library which is useful for distributed systems and cloud
services.

Advantages
GoLang is now one of the top paying programming languages and Google finances it. It is easy
to learn because it uses the new language. Since Go is close to C language, it will be easier to
learn for who are proficient in C++ and C#. It can support almost all features seen in other
languages such as inheritance, overloading, object, and others. This has excellent concurrency
patterns. The developer can finish the requests faster, freeing the resources for hardware and
network much easier. The built-in tools are easy to use which helps the developers write
maintainable and readable code. Concurrency is one of the most commendable features of
GoLang because it has a goroutine. It provides a goroutine scheduler system which makes it
easy to bring about millions of goroutines. The concurrency allows parallelism in an easier way
than is possible in other languages. It is a garbage-collected language which makes it easier for
the developer to do memory management, as the language itself takes care of most of the grunt
work needed. Go has alerts for documentation which is useful for younger programmers to
avoid falling into sloppy documentation habits early on. Since google is still one of the top
search engines and a competition for programs used in the office, Go will play a big role in
Google’s architecture for a long time.

Disadvantages
GoLang lacks frameworks and it is difficult to handle errors in it. It is still a young and developing
language which means that there are a few libraries yet that’s why developers write libraries
themselves, unlike older languages that have gathered various libraries and huge collections of
code through its enthusiastic and engaged community. Package management is a downside
because by default, it cannot create a dependency tree with a fixed package version. The build
may be based on different versions of packages when building at different times. Go has no GUI
library, thus a lot of knowledge and time is needed to connect a library to an app. It lacks a
virtual machine so complex programs written with Go quickly chew through the RAM. It has yet
to make a name for itself in the programming world where JavaScript is for front-end
programming and Python for data visualization and analysis. Go still needs to make its mark in
the wider programming ecosystem.

References:

Cheney, D. (2012, January 18). Why Go gets exceptions right. Retrieved September 18,
2020, from https://dave.cheney.net/2012/01/18/why-go-gets-exceptions-right

freeCodeCamp.org, 2020. Learn Go Programming - Golang Tutorial For Beginners. [video]


Available at: <https://www.youtube.com/results?
search_query=go+language&ab_channel=freeCodeCamp.org> [Accessed 16 September
2020].

GeeksforGeeks. 2020. Go Programming Language (Introduction) - Geeksforgeeks. [online]


Available at: <https://www.geeksforgeeks.org/go-programming-language-introduction/>
[Accessed 18 September 2020].

Nhasa, M. (2018, July 09). The Benefits of Using the Go Programming Language (AKA
Golang): Mitrais Blog. Retrieved September 18, 2020, from https://www.mitrais.com/news-
updates/the-benefits-of-using-the-go-programming-language-aka-golang/

Sidorenko, I. (2019, February 26). Should I Go? The Pros and Cons of Using Go Programming
Language. Retrieved September 18, 2020, from https://hackernoon.com/should-i-go-the-
pros-and-cons-of-using-go-programming-language-8c1daf711e46

Yegulalp, S. (2019, October 16). What's the Google Go language (Golang) really good for?
Retrieved September 18, 2020, from https://www.infoworld.com/article/3198928/whats-
the-go-language-really-good-for.html

1. Data Types
Go is a statically typed programming language, meaning that when a variable has been defined
its data type cannot be changed.

Primitive data types

Boolean - stores value that is either true or false. In Go, false is the default value and is also the
zero value.

Example: var isBool = true


var myBool bool //myBool is set to false since that is the default value
var yourBool = 1<=3 //yourBool is set to true since 1 is lesser than 3

Numerical
Signed - represents whether it is a positive or negative number. Has the same range
overall as the unsigned but has a different magnitude as half of the values are then represented
in negative.
● int - machine dependent because its size depends on the type of architecture you
are using
● int8 - 8 bits/1 byte - (-128 to 127) - -27 to 27 -1
● int16 - 16 bits/2 bytes - (-32768 to 32767) - -215 to 215 -1
● int32 / rune - 32 bits/4 bytes - (-2147483648 to 2147483647) - -231 to 231 -1
● int64 - 64 bits/8 bytes - (-9223372036854775808 to 9223372036854775807) -
-263 to 263 -1
Unsigned - unsigned integers only contain positive numbers including zero
● uint - machine dependent because its size depends on the type of architecture
you are using
● uint8 - 8 bits/1 byte - (0 to 255) - 0 to 28 -1
● uint16 - 16 bits/2 bytes (0 to 65535) - 0 to 216 -1
● uint32 - 32 bits/4 bytes (0 to 4294967295) - 0 to 232 -1
● uint64 - 64 bits/8 bytes (0 to 18446744073709551615 - 0 to 264 -1
● uintptr - an unsigned integer type that is large enough to hold any pointer
address. Therefore it is size and range are platform dependent.

int, uint, uintptr


Size:
- On 32 bit machines, the size of an int will be 32 bits or 4 bytes.
- On 64 bit machines, the size of an int will be 64 bits or 8 bytes
- On 32 bit machines, the range of int will be -231 to 231 -1.
- On 64 bit machines, the range of int will be -263 to 263 -1

Note: GoLang is a strongly typed language, so it does not allow the use of multiple numeric
types in an expression.

Example:
var x int64=10
var y int=20
var z= x+y //will output an error indicating a mismatch in type

var x int64=10
var y int=x //will output an error in assignment

Example:
var myChar byte = ‘A’ //stores ASCII characters
var myUnicode rune = ‘♥’ //stores unicode characters

fmt.Printf("%c = %d and %c = %U\n", myChar, myChar, myUnicode, myUnicode)

Output:
A = 65 and ♥= U+2665

Note: Characters in golang are stored in single quotes.

Floating Point
● float64 - 64 bits or 8 bytes, whenever a value is initialized with a decimal value
and a float type was not specified float64 will be the default type inferred
● Float32 - 32 bits or 4 bytes
Example:
var a float32 = 2

Complex
● complex64 - both real and imaginary parts are float32, real and imaginary parts
are both 32 bits or 4 bytes
● complex128 - both real and imaginary parts are float64, the default complex data
type. real and imaginary parts are both 64 bits or 8 bytes

Example:
var a float32 = 3
var b float32 = 5
c := complex(a,b)

var d complex64
d = 4+5i

Error - In Go, errors can be returned via an explicit return value. They are the last return value
with type error, a built-in interface. A nil value in the error position indicates that there was no
error. Using errors.New will construct a basic error value with the given error message.

Example:
import ”errors”
err := errors.New(“Oh no! Something went wrong..”)
if err != nil
{
fmt.Print(err)
}

Byte
In Go, byte is an alias for uint8 meaning it contains integer values of 8 bits and one byte
represents integers between 0 - 255. A single byte therefore can represent ASCII characters as
Golang does not have any data type of ‘char’ which is why byte is used to represent the ASCII
characters. We have to specify the type when declaring a byte as the default type is rune (rune
represents all UNICODE characters). ASCII defines 128 characters, identified by the code
points 0-127. It covers English letters, Latin numbers, and a few other characters. Unicode is a
superset of ASCII, defines a codespace of 1,114,112 code points.

Rune
In Go, rune is an alias for int32 meaning it contains integer values and is meant to
represent a Unicode Code Point and is the default type for all Unicode Code Points/characters
in UTF-8 format.

1. String Types
● Are strings in the language implemented as a special kind of character array or
primitive type (with no array-style subscripting operation?)
In Go, a string is, in effect, a read-only slice of bytes. It is important to state right up front
that a string holds arbitrary bytes. It is not required to hold Unicode text, UTF-8 text, or
any other predefined format. As far as a string's content is concerned, it is precisely
equivalent to a slice of bytes. It is still implemented as a primitive type because go
language has a built-in package for primitive data types. The string is the set of all
strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded
text. A string may be empty, but not null. The values of string type are immutable.
● How are strings implemented?
It is implemented through a double quotation and utf-8 and string literals if possible to
put inside. fmt.Printf. can also be used to look presentable when it is printed so that a
format can be placed inside a string when it will be printed.

Example:
package main

import (
"fmt"
)
func main() {
fmt.Println("Hello")
samp := "Hi"
fmt.Println(samp)
}
● Do strings in the language have static or dynamic length?
Go is a statically typed language meaning that each statement in the program is
checked at compile time. This means that the data type is bound to the variable,
whereas in dynamically linked languages, the data type is bound to the value.
2. Enumerate, discuss, and give examples for user-defined ordinal types. (if
applicable. Specify if not applicable)
The possibility of linking together a set of positive integers to a range of values is what we call
an ordinal type. There exist two kinds of user defined ordinal types which are enumeration and
subrange. In Golang, enumeration is possible using the keyword iota which represents
consecutive positive integer constants which starts from zero. It reverts back to the value zero
whenever the keyword const appears in the code.

Example:
const
(
C0 = iota
C1 = iota
C2 = iota
)
fmt.Println(C0,C1,C2)
Output: 0 1 2

const
(
C1 = iota +1
C2
C3
)
fmt.Println(C0,C1,C2)
Output: 1 2 3

const
(
C1 = iota +1
-
C3
C4
)
fmt.Println(C0,C1,C2)
Output: 1 3 4

3. Array Types
● discuss how arrays are initialized
An array in Golang is a collection of elements with a numbered sequence that has a specific
length. Setting values in an index by using the syntax array[index]=value, we can also get a
value placed using array[index].

Array types are one-dimensional, but you can compose types to build multidimensional data
structures. Arrays in Golang are fixed-length/static sequences that are used to store
homogeneous elements in the memory. An array is initialized through declaring the variable to
which the array is to be assigned to, followed by declaring the number of elements it can hold in
square brackets, followed by the type of elements the array holds, and followed by a list of initial
values separated by commas of each element inside the curly braces as shown in the example
below. Once the size of an array has been defined, it cannot be increased or decreased. This
means that we cannot assign an array to another array of the same type but with different
length. Also, when passing an array as a function argument, the size is also a part of it.
Subscripts of arrays in Golang are of value type int or are numerical in value and are
represented by integers starting from index 0 and depending on the compilation architecture,
system memory size limits, and hardware, it can either be of type int32 or int64. The last index
is the array’s length subtracted by 1. Specifying an index type in Go is impossible.

Example:
package main

import "fmt"

func main() {
var a [2]string //declaring the size of an array and giving the elements of the
index one by one
a[0] = "Hello"
a[1] = "World"
fmt.Println(a[0], a[1])
fmt.Println(a)

primes := [6]int{2, 3, 5, 7, 11, 13} //declaring the size of an array and giving the
elements of the index in one line
fmt.Println(primes)
}
Output:

Hello World
[Hello World]
[2 3 5 7 11 13]

Go runtime will check whether the involved indexes are out of range. If an index turns out to be
out of range, a panic is then produced in order to prevent the invalid index from causing any
harm. This is called bounds check. Bounds check makes the code run safely, but they also
make the code run slower. The error message thrown at the user is “invalid array index <index>
(out of bounds for <array length>-element array).

4. Slice
Slice in Go is similar to an array that serves as a container to hold elements of the same
data type but slice can vary in size. The syntax used is the same as that for array but
without specifying the element count. As arrays are static or fixed length sequences,
slices were implemented as a way for developers to dynamically change the size of
arrays. Declaring a slice is almost the same as declaring an array, except that you must
omit the length specifier for example: slice:= []int{1,2,3}. Unlike an array a slice is
initialized with a nil value meaning it does not allocate the memory of data blocks during
initialization.

Example:
var a int[] //here, a is a slice

Unlike an array int with size n that will have n zeroes, since zero is the default value of int, slice
will have nil as its default value. It has a value nil because a slice is just a reference to an array.
Slice does not contain data but rather stores data in an array.

S-ar putea să vă placă și