Go basic knowledge
Why Go?
Go was built at Google, a company synonymous with Search and Distributed Computing, with Kubernetes. They wanted a language that was fast, worked well with automated code review and formatting and allowed a large team to write large scale software effectively, catered to the multi-core and networking era. All other major languages are at least a decade old. They were created in an era where memory was costly, where there were no massive clusters or multi-core processors.
When switching from other languages to Go, it'll be more or less frustrating to see the restrictions Go has. But as you tag along, the nuisances pay off. The language takes care of other things like formatting, and its goal is to provide a scalable approach to build the application over a long period of time.
In the C family of languages, there are two factions:
vs
The same can be said of Python spaces/tabs.
Technically speaking, this was an unintended consequence of doing away with the semi colons, since the Go compiler adds semicolons at the end of each line, we can't have the #2 definition in Go.
This might seem to be a shallow problem, but when the codebase and team size grows, then it is difficult to maintain the consistency because of different user preferences. Anyone can write code these days, few can write elegant code. Other languages had to solve this problem as an afterthought. In Go they have been built into the language. Go isn't just a language, it is an ecosystem and it caters to the entire software development cycle.
It aims to provide the efficiency of a statically-typed compiled language with the ease of programming of a dynamic language.
A list of features:
Unused imports/variables are compiler errors.
Semi-colons not needed, the compiler adds them at the line end.
A folder $GOPATH, as it is called contains all your Go code.
There is only one standard way to write Go code, use gofmt.
Batteries included standard library.
Compiled language, thus very fast.
Webapps can be written without a framework.
Has concurrency built in, just attach the word
go
before a function call to run it in its own goroutine.Supports Unicode.
No language change from 1.0 to 1.7
Let's start with the customary Hello World.
First Program
It prints following information.
Explanation
We import the format package, fmt
for short. Write the main function of the main package and run the code. We can access only the Exported
objects of any library in Go. Exported means that the names should start with a capital letter. The compiler will find the main
function of the main
package whenever we build or execute any Go code.
We printed non ASCII characters. Go supports UTF-8 by default.
The main package
It is mandatory for each Go program to be a part of a package. The package can be main or not.
Every package except main should be a distinct folder on the $GOPATH
. Main is a special package for which having a folder on $GOPATH
is optional.
Building applications differs when we have a main folder and when we don't.
$GOPATH/src/github.com/thewhitetulip/Tasks
main.go
view.go
or
$GOPATH/src/github.com/thewhitetulip/Tasks
main/main.go
view.go
In scenarios like this, we need to understand two different ways of executing the application
With the main folder:
This will function correctly, because we are in the Tasks directory while executing our binary, all the templates and other files are present in this folder.
Without the main folder
Here, we are in the Tasks/main directory, the binary will expect all the other files in the Tasks/main directory when they are in the Tasks directory, one level up.
There can be only one main package & function per executable program. The main function takes no arguments passed and returns nothing.
Links
Last updated