SlideShare a Scribd company logo
EQUALEYES BLOG
Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month!
Beginning development in Go
Zan Skamljic
EQUALEYES BLOG
Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month!
I have heard of Go, but what is it really?
Go (sometimes referred to as “golang”) is a programming language developed by Google. If that did not
get your attention, maybe the co-author will: Ken Thompson. He implemented both Unix and the B
programming language, which was a direct predecessor of C. Go is a compiled language, using a garbage
collector. The compiler and other Go tools are all free and open source. Its compiler is self-hosting,
meaning the compiler and the tools themselves are written in Go. Go is supposed to be simple, lightweight
and clean. It only has 25 keywords!
Ok, but why should I consider trying Go?
For me, it was the cross-platform development. Go can be build for multiple architectures, multiple OS-
es in a single line. For example, a developer on a Linux machine can easily build a Windows or macOS
executable without any hassle, for example “GOOS=windows GOARCH=amd64 go build”. The GOARCH
variable only needs to be present if we’re building for a different architecture than the machine we’re
developing on, for example arm.
When building an executable, you will notice that it is bigger than usual executables. That’s because the
file is compiled as a monolithic executable. This means that the file will contain every single dependency
it needs. Essentially, this means that it won’t need any so/dll/dylib files. It creates a dependency free
executable, no need to install anything or have more than one file to copy and run!
The language has a great multi-threading concept called “goroutines”. A goroutine is a function call,
preceded by the “go” keyword. It spawns a lightweight thread in which that function is executed at almost
zero cost.
Another great feature is formatting. Many languages lack a proper formatting tool and many times they
ignore the code style altogether. Go is slightly different. The style is uniform across all applications, so
every source file is formatted the same. Several ways to format the code may also report compile-time
errors, which may seem like a downside at first, but it forces you to write readable code.
When you install Go, the tooling comes with a compiler, code formatter, package manager, and a testing
framework. This basically means that majority of tasks are as simple as running for example “go build” for
building a project, “go get” to download dependencies and “go test” to run tests.
OK, I’m sold. How do I start?
Pick your favourite editor or IDE, it probably already has a plugin for Go. If you can’t decide I would suggest
either GoLand (by JetBrains), Visual Studio Code (with Go plugin) or if you’re feeling hardcore, vim with
vim-go plugin. The installers for Go are available at https://golang.org/dl/. If you’re using Linux, the
package is probably in your repositories, so you can just use your package manager to install it (sudo apt
install go, yum install go, pacman -S go, …). The example hello world follows:
EQUALEYES BLOG
Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month!
package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
Let’s save that as “main.go”. To run it we can use the terminal command “go run” (which basically
compiles and runs the code in current directory, neat, right?). If we wanted to build an executable we
could run “go build” instead. I believe the code here should be pretty self-explanatory: we declare that
our file belongs to package main (which is the default go package), import the “fmt” package, which
contains functions for formatting strings and basic I/O. The “fmt.Println()” call basically means “call Println
from package fmt”.
How about something more complex?
Let’s create a simple HTTP server. It may sound like a lot of work, but Go has an extensive standard library,
which contains most things you’d need a third party library for in other languages. Here’s the code that
runs a http server, listening on port 8080, returning the “Hello world” message:
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
})
http.ListenAndServe(":8080", nil)
}
Impressive, isn’t it? Now naturally, there’s a way to route to multiple endpoints and filter the calls based
on the request method, but that’s a topic for another time.
EQUALEYES BLOG
Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month!
How about OOP?
OOP in Go is slightly different to what you might be used to, in fact it’s probably mostly similar to C (not
C++!). Anyone with experience in C development will notice that OOP works mostly in the same way, but
with several improvements. Let’s take a look:
type User struct {
Name string `json:"first_name"`
age int `json:"age"`
}
func (u User) PrintHello() {
fmt.Printf("Hello %sn", u.Name)
}
func (u *User) SetAge(age int) {
u.Age = age
}
// ...
user.PrintHello()
user.SetAge(23)
// ...
The code here defines a structure User, with a public field “Name” and a private field age. “But how can
you tell?” you may ask. Simple. All names in lowercase are package-private and the uppercase names are
public. We’ve also declared two functions. Notice the thing on the left of the function name? It’s called a
receiver. It can be either a pointer or a regular variable. The pointer is used when you want the function
to mutate the object or if you want to avoid copying the object. Notice that both the pointer and regular
references access the function and variables without ever using the arrow (“->”) operator like in C. You’ve
also probably noticed the “`json:"first_name"`”. These are called tags. They are accessible at runtime using
reflection, should you need them. In our case they can be used to serialize the User object to json, using
EQUALEYES BLOG
Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month!
the standard library, with the names specified in the tags. Similarly, we can also use tags to associate fields
with column names in a database for ORMs, xml serialization and many more.
How about performance?
Since Go is a compiled language it is a lot faster than the scripting languages like Node.js, Python etc. In
fact, Go is very close to C/C++ when it comes to performance (even though it offers access to runtime
reflection and a garbage collector). For example here are some sample benchmarks for Go: Go vs C, Go
vs C++, Go vs Node.js.
That’s all for now, I hope it made you consider using Go in one of your future projects.

More Related Content

What's hot

Go lang
Go langGo lang
Running a Plone product on Substance D
Running a Plone product on Substance DRunning a Plone product on Substance D
Running a Plone product on Substance D
Makina Corpus
 
ATO 2014 - So You Think You Know 'Go'? The Go Programming Language
ATO 2014 - So You Think You Know 'Go'? The Go Programming LanguageATO 2014 - So You Think You Know 'Go'? The Go Programming Language
ATO 2014 - So You Think You Know 'Go'? The Go Programming Language
John Potocny
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go lang
Amal Mohan N
 
Php test fest
Php test festPhp test fest
Php test fest
Barry O Sullivan
 
Go Programming Language by Google
Go Programming Language by GoogleGo Programming Language by Google
Go Programming Language by Google
Uttam Gandhi
 
Golang #5: To Go or not to Go
Golang #5: To Go or not to GoGolang #5: To Go or not to Go
Golang #5: To Go or not to Go
Oliver N
 
Messing with binary formats
Messing with binary formatsMessing with binary formats
Messing with binary formats
Ange Albertini
 
An introduction to go programming language
An introduction to go programming languageAn introduction to go programming language
An introduction to go programming language
Technology Parser
 
Getting started with Go - Florin Patan - Codemotion Rome 2017
Getting started with Go - Florin Patan - Codemotion Rome 2017Getting started with Go - Florin Patan - Codemotion Rome 2017
Getting started with Go - Florin Patan - Codemotion Rome 2017
Codemotion
 
The Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventureThe Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventure
mylittleadventure
 
Windows script host
Windows script hostWindows script host
Windows script host
ArghodeepPaul
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
Basil N G
 
Windows batch scripting
Windows batch scriptingWindows batch scripting
Windows batch scripting
ArghodeepPaul
 
E-books and App::Pod2Epub
E-books and App::Pod2EpubE-books and App::Pod2Epub
E-books and App::Pod2Epub
Søren Lund
 
Introduction to Go programming
Introduction to Go programmingIntroduction to Go programming
Introduction to Go programming
Exotel
 
Golang and Domain Specific Languages - Lorenzo Fontana - Codemotion Rome 2017
Golang and Domain Specific Languages - Lorenzo Fontana - Codemotion Rome 2017Golang and Domain Specific Languages - Lorenzo Fontana - Codemotion Rome 2017
Golang and Domain Specific Languages - Lorenzo Fontana - Codemotion Rome 2017
Codemotion
 
Building Command Line Tools with Golang
Building Command Line Tools with GolangBuilding Command Line Tools with Golang
Building Command Line Tools with Golang
Takaaki Mizuno
 
Introduction to PHP (SDPHP)
Introduction to PHP   (SDPHP)Introduction to PHP   (SDPHP)
Introduction to PHP (SDPHP)
Eric Johnson
 

What's hot (20)

Go lang
Go langGo lang
Go lang
 
Running a Plone product on Substance D
Running a Plone product on Substance DRunning a Plone product on Substance D
Running a Plone product on Substance D
 
ATO 2014 - So You Think You Know 'Go'? The Go Programming Language
ATO 2014 - So You Think You Know 'Go'? The Go Programming LanguageATO 2014 - So You Think You Know 'Go'? The Go Programming Language
ATO 2014 - So You Think You Know 'Go'? The Go Programming Language
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go lang
 
Php test fest
Php test festPhp test fest
Php test fest
 
Go Programming Language by Google
Go Programming Language by GoogleGo Programming Language by Google
Go Programming Language by Google
 
Golang #5: To Go or not to Go
Golang #5: To Go or not to GoGolang #5: To Go or not to Go
Golang #5: To Go or not to Go
 
Messing with binary formats
Messing with binary formatsMessing with binary formats
Messing with binary formats
 
An introduction to go programming language
An introduction to go programming languageAn introduction to go programming language
An introduction to go programming language
 
Getting started with Go - Florin Patan - Codemotion Rome 2017
Getting started with Go - Florin Patan - Codemotion Rome 2017Getting started with Go - Florin Patan - Codemotion Rome 2017
Getting started with Go - Florin Patan - Codemotion Rome 2017
 
The Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventureThe Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventure
 
Windows script host
Windows script hostWindows script host
Windows script host
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Windows batch scripting
Windows batch scriptingWindows batch scripting
Windows batch scripting
 
E-books and App::Pod2Epub
E-books and App::Pod2EpubE-books and App::Pod2Epub
E-books and App::Pod2Epub
 
Introduction to Go programming
Introduction to Go programmingIntroduction to Go programming
Introduction to Go programming
 
Golang and Domain Specific Languages - Lorenzo Fontana - Codemotion Rome 2017
Golang and Domain Specific Languages - Lorenzo Fontana - Codemotion Rome 2017Golang and Domain Specific Languages - Lorenzo Fontana - Codemotion Rome 2017
Golang and Domain Specific Languages - Lorenzo Fontana - Codemotion Rome 2017
 
Cpb2010
Cpb2010Cpb2010
Cpb2010
 
Building Command Line Tools with Golang
Building Command Line Tools with GolangBuilding Command Line Tools with Golang
Building Command Line Tools with Golang
 
Introduction to PHP (SDPHP)
Introduction to PHP   (SDPHP)Introduction to PHP   (SDPHP)
Introduction to PHP (SDPHP)
 

Similar to Beginning development in go

Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
Ganesh Samarthyam
 
Getting started with go - Florin Patan - Codemotion Milan 2016
Getting started with go - Florin Patan - Codemotion Milan 2016Getting started with go - Florin Patan - Codemotion Milan 2016
Getting started with go - Florin Patan - Codemotion Milan 2016
Codemotion
 
Introduction to Go
Introduction to GoIntroduction to Go
Introduction to Go
Simon Hewitt
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
Ganesh Samarthyam
 
Go programing language
Go programing languageGo programing language
Go programing language
Ramakrishna kapa
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
Amr Hassan
 
Advantages of golang development services & 10 most used go frameworks
Advantages of golang development services & 10 most used go frameworksAdvantages of golang development services & 10 most used go frameworks
Advantages of golang development services & 10 most used go frameworks
Katy Slemon
 
Scaling applications with go
Scaling applications with goScaling applications with go
Scaling applications with go
Vimlesh Sharma
 
C++ for hackers
C++ for hackersC++ for hackers
C++ for hackers
Franciny Salles
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01Getachew Ganfur
 
Cross Platform Objective C Development Using Gn Ustep
Cross Platform Objective C Development Using Gn UstepCross Platform Objective C Development Using Gn Ustep
Cross Platform Objective C Development Using Gn Ustepwangii
 
C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it security
CESAR A. RUIZ C
 
Opensource Software usability
Opensource Software usabilityOpensource Software usability
Opensource Software usability
Giacomo Antonino Fazio
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)
Jorge López-Lago
 
Resources For Floss Projects
Resources For Floss ProjectsResources For Floss Projects
Resources For Floss Projects
Jon Spriggs
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 
Introduction to Google's Go programming language
Introduction to Google's Go programming languageIntroduction to Google's Go programming language
Introduction to Google's Go programming language
Mario Castro Contreras
 
Scripting in OpenOffice.org
Scripting in OpenOffice.orgScripting in OpenOffice.org
Scripting in OpenOffice.org
Alexandro Colorado
 
Java And Community Support
Java And Community SupportJava And Community Support
Java And Community Support
William Grosso
 
The Go features I can't live without, 2nd round
The Go features I can't live without, 2nd roundThe Go features I can't live without, 2nd round
The Go features I can't live without, 2nd round
Rodolfo Carvalho
 

Similar to Beginning development in go (20)

Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Getting started with go - Florin Patan - Codemotion Milan 2016
Getting started with go - Florin Patan - Codemotion Milan 2016Getting started with go - Florin Patan - Codemotion Milan 2016
Getting started with go - Florin Patan - Codemotion Milan 2016
 
Introduction to Go
Introduction to GoIntroduction to Go
Introduction to Go
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Go programing language
Go programing languageGo programing language
Go programing language
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
 
Advantages of golang development services & 10 most used go frameworks
Advantages of golang development services & 10 most used go frameworksAdvantages of golang development services & 10 most used go frameworks
Advantages of golang development services & 10 most used go frameworks
 
Scaling applications with go
Scaling applications with goScaling applications with go
Scaling applications with go
 
C++ for hackers
C++ for hackersC++ for hackers
C++ for hackers
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01
 
Cross Platform Objective C Development Using Gn Ustep
Cross Platform Objective C Development Using Gn UstepCross Platform Objective C Development Using Gn Ustep
Cross Platform Objective C Development Using Gn Ustep
 
C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it security
 
Opensource Software usability
Opensource Software usabilityOpensource Software usability
Opensource Software usability
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)
 
Resources For Floss Projects
Resources For Floss ProjectsResources For Floss Projects
Resources For Floss Projects
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
 
Introduction to Google's Go programming language
Introduction to Google's Go programming languageIntroduction to Google's Go programming language
Introduction to Google's Go programming language
 
Scripting in OpenOffice.org
Scripting in OpenOffice.orgScripting in OpenOffice.org
Scripting in OpenOffice.org
 
Java And Community Support
Java And Community SupportJava And Community Support
Java And Community Support
 
The Go features I can't live without, 2nd round
The Go features I can't live without, 2nd roundThe Go features I can't live without, 2nd round
The Go features I can't live without, 2nd round
 

Recently uploaded

Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 

Recently uploaded (20)

Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 

Beginning development in go

  • 1. EQUALEYES BLOG Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month! Beginning development in Go Zan Skamljic
  • 2. EQUALEYES BLOG Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month! I have heard of Go, but what is it really? Go (sometimes referred to as “golang”) is a programming language developed by Google. If that did not get your attention, maybe the co-author will: Ken Thompson. He implemented both Unix and the B programming language, which was a direct predecessor of C. Go is a compiled language, using a garbage collector. The compiler and other Go tools are all free and open source. Its compiler is self-hosting, meaning the compiler and the tools themselves are written in Go. Go is supposed to be simple, lightweight and clean. It only has 25 keywords! Ok, but why should I consider trying Go? For me, it was the cross-platform development. Go can be build for multiple architectures, multiple OS- es in a single line. For example, a developer on a Linux machine can easily build a Windows or macOS executable without any hassle, for example “GOOS=windows GOARCH=amd64 go build”. The GOARCH variable only needs to be present if we’re building for a different architecture than the machine we’re developing on, for example arm. When building an executable, you will notice that it is bigger than usual executables. That’s because the file is compiled as a monolithic executable. This means that the file will contain every single dependency it needs. Essentially, this means that it won’t need any so/dll/dylib files. It creates a dependency free executable, no need to install anything or have more than one file to copy and run! The language has a great multi-threading concept called “goroutines”. A goroutine is a function call, preceded by the “go” keyword. It spawns a lightweight thread in which that function is executed at almost zero cost. Another great feature is formatting. Many languages lack a proper formatting tool and many times they ignore the code style altogether. Go is slightly different. The style is uniform across all applications, so every source file is formatted the same. Several ways to format the code may also report compile-time errors, which may seem like a downside at first, but it forces you to write readable code. When you install Go, the tooling comes with a compiler, code formatter, package manager, and a testing framework. This basically means that majority of tasks are as simple as running for example “go build” for building a project, “go get” to download dependencies and “go test” to run tests. OK, I’m sold. How do I start? Pick your favourite editor or IDE, it probably already has a plugin for Go. If you can’t decide I would suggest either GoLand (by JetBrains), Visual Studio Code (with Go plugin) or if you’re feeling hardcore, vim with vim-go plugin. The installers for Go are available at https://golang.org/dl/. If you’re using Linux, the package is probably in your repositories, so you can just use your package manager to install it (sudo apt install go, yum install go, pacman -S go, …). The example hello world follows:
  • 3. EQUALEYES BLOG Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month! package main import "fmt" func main() { fmt.Println("Hello world") } Let’s save that as “main.go”. To run it we can use the terminal command “go run” (which basically compiles and runs the code in current directory, neat, right?). If we wanted to build an executable we could run “go build” instead. I believe the code here should be pretty self-explanatory: we declare that our file belongs to package main (which is the default go package), import the “fmt” package, which contains functions for formatting strings and basic I/O. The “fmt.Println()” call basically means “call Println from package fmt”. How about something more complex? Let’s create a simple HTTP server. It may sound like a lot of work, but Go has an extensive standard library, which contains most things you’d need a third party library for in other languages. Here’s the code that runs a http server, listening on port 8080, returning the “Hello world” message: func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello world!") }) http.ListenAndServe(":8080", nil) } Impressive, isn’t it? Now naturally, there’s a way to route to multiple endpoints and filter the calls based on the request method, but that’s a topic for another time.
  • 4. EQUALEYES BLOG Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month! How about OOP? OOP in Go is slightly different to what you might be used to, in fact it’s probably mostly similar to C (not C++!). Anyone with experience in C development will notice that OOP works mostly in the same way, but with several improvements. Let’s take a look: type User struct { Name string `json:"first_name"` age int `json:"age"` } func (u User) PrintHello() { fmt.Printf("Hello %sn", u.Name) } func (u *User) SetAge(age int) { u.Age = age } // ... user.PrintHello() user.SetAge(23) // ... The code here defines a structure User, with a public field “Name” and a private field age. “But how can you tell?” you may ask. Simple. All names in lowercase are package-private and the uppercase names are public. We’ve also declared two functions. Notice the thing on the left of the function name? It’s called a receiver. It can be either a pointer or a regular variable. The pointer is used when you want the function to mutate the object or if you want to avoid copying the object. Notice that both the pointer and regular references access the function and variables without ever using the arrow (“->”) operator like in C. You’ve also probably noticed the “`json:"first_name"`”. These are called tags. They are accessible at runtime using reflection, should you need them. In our case they can be used to serialize the User object to json, using
  • 5. EQUALEYES BLOG Follow Equaleyes on social media and join our newsletter for fresh articles on tech and startups every month! the standard library, with the names specified in the tags. Similarly, we can also use tags to associate fields with column names in a database for ORMs, xml serialization and many more. How about performance? Since Go is a compiled language it is a lot faster than the scripting languages like Node.js, Python etc. In fact, Go is very close to C/C++ when it comes to performance (even though it offers access to runtime reflection and a garbage collector). For example here are some sample benchmarks for Go: Go vs C, Go vs C++, Go vs Node.js. That’s all for now, I hope it made you consider using Go in one of your future projects.