Skip to content

Latest commit

 

History

History
105 lines (64 loc) · 10 KB

File metadata and controls

105 lines (64 loc) · 10 KB

What makes Go different from other languages?

The Go programming language was created with one goal in mind, to be able to build scalable web-applications for large scale audiences in a large team. So that is the reason they made the language as standardized as possible, hence the gofmt tool and the strict usage guidelines to the language was for the sake of not having two factions in the developer base, in other languages there are religious wars on where to keep the opening brace?

    public static void main() {

    }

    or
    public static void main()
    {

    }

or for python should we use 4 spaces or 6 spaces or a tab or two tabs and other user preferences. If you know python then you might be aware of PEP8, which is a set of guidelines about how to write elegant code.

While this might seem to be a shallow problem, when the codebase grows and more and more people work on the same code base it is becomes increasingly difficult to maintain the code's "beauty." We live in a world where robots can drive a car, so we shouldn't just write code, we should write elegant code.

For other languages there are many variables when it comes to writing code. Every language is good for its use case, but Go is a little special because it was designed at a company which is the very synonym of the Internet (and distributed computing). Typically in order to optimize programs, developers choose to write Java over Python and C++ over Java, but almost all available languages widely in use were written decades ago when 1GB storage was much pricier. Now storage and computing is relatively cheap and computers are getting multiples cores, but the "old languages" are not harnessing concurrency in a way that go does. It's not because those languages are bad; utilizing concurrency wasn't a relevant usecase while the older languages evolved.

To mitigate all the problems that Google faced with current tools, they wrote a systems language called Go which you are about to learn! There are many advantages to using golang and there are disadvantages too, for every coin has both sides. One of the significant improvements is in code formatting. Google has designed the language to avoid debates on code formatting. Go code written by anyone in the world (assuming they know and use gofmt) will look exactly the same. This won't seem to matter until you work in a team! Also when the company uses automated code review or some other fancy technique, the formatted code may break in other languages which don't have strict and standard formatting rules, but not in go!

Go was designed with concurrency in mind, please note that parallelism != concurrency, there is an amazing post by Rob Pike on the golang blog, you will find it there, it is worth a read.

Another very important change that is the concept of GOPATH. Gone are the days when you had to create a folder called code and then create workspaces for eclipse and what not. Now you have to keep one folder tree for go code which will be updated by the package manager automatically. It is also recommended to create folders with either a custom domain or the GitHub domain, for example I created a task manager using golang so I created a set of folders ~/go/src/github.com/thewhitetulip/Tasks

Note: In *nix systems ~ stands for home directory, which is the windows equivalent of C:\\Users\\username. Now the ~/go/ is the universe for the gocode in your machine. This is a significant improvement over other languages; we can store the code efficiently without hassles. While it might seem strange at first, this approach make a lot of sense than the ridiculous package names, i.e. package names generated for other languages using reverse domains.

Note: Along with src there are two folders pkg which is for packages and bin which is for binary.

This GOPATH advantage isn't just restricted to storing code in particular folder. When you have created five packages for your project, you don't have to import them like "import ./db". Instead you can use import "github.com/thewhitetulip/Tasks/db" so that when executing go get on my repo, the go tool will find the package from github.com/... path if it wasn't downloaded initially. This standardizes a lot of screwed up things in the programming discipline. (<-- To remove and replace with actual explanation of why this is better)

While there may be some founded complaints that go creators have ignored all language research done since the past 30yrs, you cannot create a product or a language which everyone will fall in love with. There are always some or the other use cases or constraints which the creators should consider. Considering all the advantages at least for web development I do not think any language gets close to the advantages which go has even if you ignore all that I said above. Go is a compiled language which means in production, you won't have to setup a JVM or a virtualenv and will instead have a single static binary! Like an icing on a cake, all the modern libraries are in the standard library, such as the http lib, allowing you to create webapps in golang without using a third party web framework.

2.1 Hello, Go

Before we start building an application in Go, we need to learn how to write a simple program. You can't expect to build a building without first knowing how to build its foundation. Therefore, we are going to learn the basic syntax to run some simple programs in this section.

Program

According to international practice, before you learn how to program in some languages, you will want to know how to write a program to print "Hello world".

Are you ready? Let's Go!

	package main

	import "fmt"

	func main() {
		fmt.Printf("Hello, world or 你好,世界 or Καλημέρα κόσμε or こんにちは世界\n")
	}

It prints following information.

Hello, world or 你好,世界 or Καλημέρα κόσμε or こんにちは世界

Explanation

One thing that you should know in the first is that Go programs are composed by package.

package <pkgName> (In this case is package main) tells us this source file belongs to main package, and the keyword main tells us this package will be compiled to a program instead of package files whose extensions are .a.

Every executable program has one and only one main package, and you need an entry function called main without any arguments or return values in the main package.

In order to print Hello, world…, we called a function called Printf. This function is coming from fmt package, so we import this package in the third line of source code, which is import "fmt"

The way to think about packages in Go is similar to Python, and there are some advantages: Modularity (break up your program into many modules) and reusability (every module can be reused in many programs). We just talked about concepts regarding packages, and we will make our own packages later.

On the fifth line, we use the keyword func to define the main function. The body of the function is inside of {}, just like C, C++ and Java.

As you can see, there are no arguments. We will learn how to write functions with arguments in just a second, and you can also have functions that have no return value or have several return values.

On the sixth line, we called the function Printf which is from the package fmt. This was called by the syntax <pkgName>.<funcName>, which is very like Python-style.

As we mentioned in chapter 1, the package's name and the name of the folder that contains that package can be different. Here the <pkgName> comes from the name in package <pkgName>, not the folder's name.

You may notice that the example above contains many non-ASCII characters. The purpose of showing this is to tell you that Go supports UTF-8 by default. You can use any UTF-8 character in your programs.

Each go file is in some package, and that package should be a distinct folder in the GOPATH, but main is a special package which doesn't require a main folder. This is one aspect which they left out for standardization! But should you choose to make a main folder then you have to ensure that you run the binary properly. Also one go code can't have more than one main go file.

~/go/src/github.com/thewhitetulip/Tasks/main $ go build ~/go/src/github.com/thewhitetulip/Tasks $ ./main/main

the thing here is that when your code is using some static files or something else, then you ought to run the binary from the root of the application as we see in the second line above, I am running the main binary outside the main package, sometimes you might wonder why your application isn't working then this might be one of the possible problems, please keep this in mind.

One thing you will notice here is that go doesn't see to use semi colons to end a statement, well, it does, just there is a minor catch, the programmer isn't expected to put semi colons, the compiler adds semi colons to the gocode when it compiles which is the reason that this (thankfully!) is a syntax error

    func main ()
    {
    }

because the compiler adds a semi colon at the end of main() which is a syntax error and as stated above, it helps avoid religious wars, i wish they combine vim and emacs and create a universal editor which'll help save some more wars! But for now we'll learn Go.

Conclusion

Go uses package (like modules in Python) to organize programs. The function main.main() (this function must be in the main package) is the entry point of any program. Go standardizes language and most of the programming methodology, saving time of developers which they'd have wasted in religious wars. There can be only one main package and only one main function inside a go main package. Go supports UTF-8 characters because one of the creators of Go is a creator of UTF-8, so Go has supported multiple languages from the time it was born.

Links