Doug Hieber, Senior Application Architect
April 30, 2018

If you haven’t heard of Google’s Go language, the author of this post provides an overview to get you started.


Google Go GopherLast year, I joined the development team at Strasz that is responsible for the Reg! system which allows candidates to register for various types of exams online. I was pretty excited. Reg! is part of the recent innovations at Strasz, and it is great to work on a growing product. The work team atmosphere is filled with energy, collaboration and new ideas. Each day brings the ability to have an impact in a way that makes me eager to come to work.

As a tech guy, however, I was also excited to learn a new programming language: Google’s Go1 sometimes referred to as golang. Cool stuff! It even has its own mascot, the Go gopher2.

The good news was, it was pretty easy to absorb it. After a couple Pluralsight courses , some online3 research, and some experimenting, and I was ready to go. No pun intended.

Once I was on solid footing, I shared my newfound understanding with others outside of the Reg! development team. This created enough of a buzz that I thought I should share a few of my thoughts with a broader audience.

First of all, Go is open source and available for download from the Go site. It is a compiled language, statically typed, case sensitive and has automatic garbage collection. The syntax is similar to C and easy for anyone with a coding background to pick up. In fact, the goal of its creators was to keep the syntax simpler, safer and more concise than C, C#, or Java. Here is a simple code snippet:

var x
var a int = 10
fmt.Println(a, ” is pre-loop value”)
for a : = 1; a < 8; a++ {
if a%2 == 0 {
fmt.Println(a, ” is even”)
}
}

Output: 10 is pre-loop value, 2 is even, 4 is even, 6 is even

You probably noticed some subtle differences from other C-like languages, such as no parenthesis. And “:=”, which is shorthand for declare and assign, so my 2 nd line could have been simply “a := 1”. Also, Go does not use semi-colons in the source code to indicate the end of a line. Instead, the lexer determines where a statement ends. A collateral effect of this is that “if” statements MUST have the opening brace on the same line as the “if”. Otherwise, the code block will not be associated with the “if”.

Also, the compiler is fairly strict in an effort to keep the code clean and uncluttered. For example, the code snippet above would generate a compiler error because the variable “x” is not used anywhere.

Go is not an object oriented language. That said, it does have the concept of interfaces in order to define a set of methods. You can then create a custom data type using “struct”, and if you implement all the methods in the interface, that custom data type will conform to the interface. Instances of that custom data type can then be passed into methods expecting the interface, as a type of polymorphic dispatch. Confused? This example page should help.

That said, Go does support anonymous functions and the creation of closures. And Go functions are first class functions, meaning they can be used in the same manner as objects. These features can be used to form class-like constructs that wrap both functions and lexically scoped variables, and then create instances of those constructs. The result can be an OO-like design similar to how OO Javascript is done. In the following example, notice how an instance of the “adder” is created and maintains its state, much like a class.

func adder() func(int) int {
currentNum := 0
return func(increaseBy int) int {
currentNum += increaseBy
}
}

func main() {
myAdder := adder()
fmt.Println(myAdder(2))
fmt.Println(myAdder(5))
}

Output: 2, 7

The creators’ desire to stay simple also means no generics, assertions or implicit type conversions. Initially, it did not even have exceptions, but later versions added “panic” to trigger critical errors and “recover” to catch them. Yes, the actual keyword is “panic”. While that is somewhat amusing, it conveys that it should be used for truly serious errors, and not to handle application logic. For example:

if databaseConnection == nil {
panic(“The database is down!”)
}

Output: “panic: The database is down!”

Function parameters are passed by value, with the exception of “map” (a type of hash table) and “slice” (a type of array). Pointers can also be used and passed into functions. And a unique, cool feature is that functions can return multiple values. This is typically used to return a result and an error code, but it can be used for anything. For example:

func getTen() (int, string) {
return 10, “this is hard coded”
}

func main() {
a, b := getTen()
fmt.Println(a, b)
}

Output: 10 this is hard coded

There are several ways to compile and run a Go program. You can usa a command line as simple as “go run main.go” or “go build main.go”. Or better, you can use an IDE, like GoLand. But for those just looking for an area to quickly try things out, you can use the online golang sandbox.4

Using Go on our Reg! system lived up to what I had read online:

  • Go gracefully handles concurrency / parallel processing.
  • Request processing speed is a strength (as supported by benchmark tests).
  • Go is a good match for fast, easy development of web applications.

Now that you have a sense of what Go is all about, I would encourage you to dig deeper. Pluralsight is a great starting point. With just a little research you can get started writing code.

I say, just Go for it. OK, that time the pun WAS intended.

Recommended further reading:
https://blog.masterofcode.com/an-overview-on-golang-programming-language/
https://golang.org/dl/
https://gobyexample.com/
https://blog.golang.org/defer-panic-and-recover
https://www.jetbrains.com/go/
https://play.golang.org/

REFERENCES:
1 Go: https://golang.org/
2 Go Gopher: https://blog.golang.org/gopher
3 Pluralsight course: https://www.pluralsight.com/courses/go-fundamentals
4 Golang Sandbox: https://play.golang.org/

Doug Hieber, Test Developer, Senior Application Architect
April 30, 2018

If you haven’t heard of Google’s Go language, the author of this post provides an overview to get you started.


Google Go GopherLast year, I joined the development team at Strasz that is responsible for the Reg! system which allows candidates to register for various types of exams online. I was pretty excited. Reg! is part of the recent innovations at Strasz, and it is great to work on a growing product. The work team atmosphere is filled with energy, collaboration and new ideas. Each day brings the ability to have an impact in a way that makes me eager to come to work.

As a tech guy, however, I was also excited to learn a new programming language: Google’s Go1 sometimes referred to as golang. Cool stuff! It even has its own mascot, the Go gopher2.

The good news was, it was pretty easy to absorb it. After a couple Pluralsight courses , some online3 research, and some experimenting, and I was ready to go. No pun intended.

Once I was on solid footing, I shared my newfound understanding with others outside of the Reg! development team. This created enough of a buzz that I thought I should share a few of my thoughts with a broader audience.

First of all, Go is open source and available for download from the Go site. It is a compiled language, statically typed, case sensitive and has automatic garbage collection. The syntax is similar to C and easy for anyone with a coding background to pick up. In fact, the goal of its creators was to keep the syntax simpler, safer and more concise than C, C#, or Java. Here is a simple code snippet:

var x
var a int = 10
fmt.Println(a, ” is pre-loop value”)
for a : = 1; a < 8; a++ {
if a%2 == 0 {
fmt.Println(a, ” is even”)
}
}

Output: 10 is pre-loop value, 2 is even, 4 is even, 6 is even

You probably noticed some subtle differences from other C-like languages, such as no parenthesis. And “:=”, which is shorthand for declare and assign, so my 2 nd line could have been simply “a := 1”. Also, Go does not use semi-colons in the source code to indicate the end of a line. Instead, the lexer determines where a statement ends. A collateral effect of this is that “if” statements MUST have the opening brace on the same line as the “if”. Otherwise, the code block will not be associated with the “if”.

Also, the compiler is fairly strict in an effort to keep the code clean and uncluttered. For example, the code snippet above would generate a compiler error because the variable “x” is not used anywhere.

Go is not an object oriented language. That said, it does have the concept of interfaces in order to define a set of methods. You can then create a custom data type using “struct”, and if you implement all the methods in the interface, that custom data type will conform to the interface. Instances of that custom data type can then be passed into methods expecting the interface, as a type of polymorphic dispatch. Confused? This example page should help.

That said, Go does support anonymous functions and the creation of closures. And Go functions are first class functions, meaning they can be used in the same manner as objects. These features can be used to form class-like constructs that wrap both functions and lexically scoped variables, and then create instances of those constructs. The result can be an OO-like design similar to how OO Javascript is done. In the following example, notice how an instance of the “adder” is created and maintains its state, much like a class.

func adder() func(int) int {
currentNum := 0
return func(increaseBy int) int {
currentNum += increaseBy
}
}

func main() {
myAdder := adder()
fmt.Println(myAdder(2))
fmt.Println(myAdder(5))
}

Output: 2, 7

The creators’ desire to stay simple also means no generics, assertions or implicit type conversions. Initially, it did not even have exceptions, but later versions added “panic” to trigger critical errors and “recover” to catch them. Yes, the actual keyword is “panic”. While that is somewhat amusing, it conveys that it should be used for truly serious errors, and not to handle application logic. For example:

if databaseConnection == nil {
panic(“The database is down!”)
}

Output: “panic: The database is down!”

Function parameters are passed by value, with the exception of “map” (a type of hash table) and “slice” (a type of array). Pointers can also be used and passed into functions. And a unique, cool feature is that functions can return multiple values. This is typically used to return a result and an error code, but it can be used for anything. For example:

func getTen() (int, string) {
return 10, “this is hard coded”
}

func main() {
a, b := getTen()
fmt.Println(a, b)
}

Output: 10 this is hard coded

There are several ways to compile and run a Go program. You can usa a command line as simple as “go run main.go” or “go build main.go”. Or better, you can use an IDE, like GoLand. But for those just looking for an area to quickly try things out, you can use the online golang sandbox.4

Using Go on our Reg! system lived up to what I had read online:

  • Go gracefully handles concurrency / parallel processing.
  • Request processing speed is a strength (as supported by benchmark tests).
  • Go is a good match for fast, easy development of web applications.

Now that you have a sense of what Go is all about, I would encourage you to dig deeper. Pluralsight is a great starting point. With just a little research you can get started writing code.

I say, just Go for it. OK, that time the pun WAS intended.

Recommended further reading:
https://blog.masterofcode.com/an-overview-on-golang-programming-language/
https://golang.org/dl/
https://gobyexample.com/
https://blog.golang.org/defer-panic-and-recover
https://www.jetbrains.com/go/
https://play.golang.org/

REFERENCES:
1 Go: https://golang.org/
2 Go Gopher: https://blog.golang.org/gopher
3 Pluralsight course: https://www.pluralsight.com/courses/go-fundamentals
4 Golang Sandbox: https://play.golang.org/