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.

While this might seem to be a shallow problem at the top, but when the codebase grows and more and more people are working on the same code base, then it is difficult to maintain the code's "beauty", if you know python then you might be aware of PEP8, which is a set of guidelines about how to write elegant code. 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 in that turf because it was designed at a company which is the very synonym of the Internet (and distributed computing), typically the flow of writing code goes from Python to Java to C++ for optimization purposes, but the problem is that almost all languages which are widely in use right now were written decades ago when 1GB storage came at a much higher price compared to now, where storage and computing has gotten cheap. Computers are getting multiples cores these days and the "old languages" don't harness concurrency in a way that go does, not because those languages are bad, but simply because that usecase wasn't relevant when the languages evolved.

So to mitigate all the problems that Google faced with the current tools, they wrote a systems language called Go, which you are about to learn! There are many advantages to using golang, and there might be disadvantages too for every coin has both sides. But significant improvements in places like code formatting, since they designed the language in such a way that there won't be wars on how to format code, the gocode 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 then in other languages which don't have strict and standard formatting rules then the code might get screwed up, 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, blog.golang.org, you will find it there, it is worth a read.

Another very important change that go has brought in programming that I personally love 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 and it'll be updated by the package manager automatically. Also under the code we are 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, it is just a significant improvement over other languages so we can store the code efficiently without hassles, it might seem strange at first, but it does make a lot of sense over the ridiculous package names some other languages use like 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, but when you have created five packages for your project then you don't have to import them like "import ./db", you can give it import "github.com/thewhitetulip/Tasks/db", so while doing a go get on my repo, the go tool will find the package from github.com/... path if it wasn't downloaded initially, it just standardizes a lot of screwed up things in the programming discipline.

While some complain that go creators have ignored all language research done since the past 30yrs, well, it might be true, but then again you can' 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, and 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'll not setup a JVM or a virtualenv you will have a single static binary! And like an icing on a cake, all the modern libraries are in the standard library, like the http, which is a major advantage, which is the reason you can 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.

results matching ""

    No results matching ""