357

About Go Language — An Overview – I’m Learning Go (Golang) – Medium

 6 years ago
source link: https://medium.com/im-learning-go-golang/about-go-language-an-overview-f0bee143597c
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

About Go Language — An Overview

This will be a series of tutorials about Go language. The first post is a bit boring and talks about the current Go ecosystem and the language’s overview. As well as its advantages and disadvantages.

I’d been working with Node.js for many years and then with Go within my latest projects. I’ve been programming since 1992. I want to share what I learned about Go with you.

I’ll go into the details of each piece of information here in this post afterward, in the next posts. This post is here for giving you a quick overview of Go without going into the language-specific details and tutorials.

Open Source Language

Go is an open source programming language from Google. Made its first stable release, 2011.

What does it mean that Go is an open source programming language?
Well, it’s created by Google as I said. However, even you can contribute to it by creating new proposals, fixing bugs, making it faster. It’s like a living being growing in front of you. If you’re curious, its source code is hosted here on Github.

Who created Go?

Robert Griesemer, Rob Pike and Ken Thompson. They started designing Go language around 2007. Go language open-sourced in 2009. Read more here.

Go design inspired from languages like Algol, Pascal, C, Modula, Oberon, Smalltalk and Newsqueak.

Go inherited mostly from Oberon language and its syntax is from C. Go’s OOP is more like Smalltalk but in Go, you can attach methods to any type. And concurrency is mostly from Newsqueak which is a language also from Rob Pike and heavily inspired by Hoare’s paper of CSP (Communicating Sequential Processes).

Why they created Go?

Fast languages like C, are difficult to work with and not safe. Compiling speed, dependencies and runtime errors are vast. An interpreted language like Ruby is safe but it’s slower and has many dependencies, one of them is the interpreter itself. Also, for Java, for example, a virtual machine is needed to run the code. Javascript and Node.js are wild kids; which are interpreted, weakly-typed and unsafe to work with (although there are some possible directions like TypeScript or compiling directly to Javascript from other safer languages).

Also, as an example, Java became too complex and verbose to write. There are many keywords which can be guessed from the context the language constructs inside in (which is called inferring). Ruby is joyful to work with however it’s not designed for speed in mind. Javascript lets you free, go wild and slowly kills you (maintenance nightmare, callback hell (the world before async await), no built-in solutions for safety).

For example, C language compiles very quickly, however, the language itself has not been designed to be compiled very fast (I’m not talking about the compiler here), so, C programmers can misuse the language facilities to create slow compiling programs. In Go, however, it’s been designed for fast compilation in mind from the beginning. So, it’s hard for Go programmers to create slow compiling programs as compared to other languages like C or C++.

Go removes all of these obstacles like safety, speed, and ease of programming

  • Fast results: It works like an interpreted language because of the fast compilation. You’ll not notice that it’s compiling. You’ll think that as if you’re working in an interpreted language like Ruby.
  • Safe: Strongly and statically typed and garbage collected. Strongly typed means: You can’t pass any type of data everywhere. You need to be explicit. Statically typed means: Compiler knows the type of every variable. In Go there are no implicit type conversions, for example, uint8 and uint16 are different types (except in some cases).
  • Easy to work with: It’s concise, explicit and easy to read.
  • Modern: Built-in support in the language itself for multi-core networked distributed applications and more.

Go has idioms

  • “Get the job done”
  • “One way of doing things”: It’s called idiomatic Go code.
  • “Being explicit”: Explicitness is favored even if it’s not DRY. Duplication is allowed sometimes.
  • “Build things by composing them”: Do not inherit from other things, compose systems from simpler components. Although, it “inherits” this mantra from Unix philosophy.

Who uses Go?

There are at least half a million programmers in the Go community.

Most notable companies are Google, Docker, Dropbox, Heroku, Medium, Lyft, Uber, and others.

1*8x7W-7b9u4LNLZORGITktg.png?q=20
about-go-language-an-overview-f0bee143597c
Percentage of Stack Overflow questions for Go
1*oQq_wAtVl9Qj5FuxiRz5JQ.png?q=20
about-go-language-an-overview-f0bee143597c
Most used and wanted languages in 2017
1*zTvPJ0rZal-hpRAZTuAA1g.png?q=20
about-go-language-an-overview-f0bee143597c
By GitHub Pull Requests
1*4vS5bN0DhyphceehDnp8ng.png?q=20
about-go-language-an-overview-f0bee143597c
https://www.tiobe.com/tiobe-index/
1*xkb72n6RoNrxHFzhsk9Rfg.png?q=20
about-go-language-an-overview-f0bee143597c
1*EVPbxI2UjWUN6qRntxZC7w.png?q=20
about-go-language-an-overview-f0bee143597c
https://insights.stackoverflow.com/survey/2017#most-loved-dreaded-and-wanted
1*b3yR69GJsvjs_NbGM66FRA.png?q=20
about-go-language-an-overview-f0bee143597c
https://octoverse.github.com/

Also see: Go 2016 Survey Results.

Some advantages of using Go:

Go has the cutest mascot ever. OK…, Let’s see the real advantages.

Compiled

  • There is no VM. It compiles directly to the machine code (if we exclude Go’s intermediary assembly) which is fast, fast and fast (did I say fast?).
  • Fast compilation. The programming language design is built for fast compilation in mind from the beginning.
  • Compiles cross-platform to OS X, Linux, Windows, 👉 and many others.
  • Creates only one executable file output after the compilation without any dependencies, so that you can upload it anywhere which Go supports and just run it. Or just compile it there after you upload the code. No dependency hell.
  • Strong and static typed.
  • Garbage collected. It cleans up your dirt after you and integrates the whole garbage collection system into your executable binary.
  • Reliable. You can really create a very reliable software with Go. Because the inherent language design prevents you from doing awful stuff with it. 👉 For example, It has pointers but they’re mostly not as dangerous as in C because the memory is being managed by Go and pointer arithmetic is not advised by default.
    👉However, this reliability is only for the compilation part, in runtime, nasty things can happen, if you want maximum run-time reliability, you may prefer, for example, Rust instead.

Paradigms

  • It’s an imperative language which is an advantage and a disadvantage to some people.
  • Supports a different kind of object-oriented programming (OOP). I come from many OOP languages like Java, C#, Ruby, but, Go gets the best practices from OOP and lets you program differently, in a Go way.

Go wants you to compose things not inherit like in other OOP langs.

  • Supports interfaces (as in OOP). This helps to compose things. Does “Polymorphism” ring a bell? Go does not force you to mark your types as “implements this and that interface”, it infers this from the functionality the type supports. This increases flexibility and composability.
  • Go lets you attach functions to any type. This flexibility lets you compose your programs from smaller things. When a type implements the functions of an interface it’s said that the type satisfies that interface and can be used in places that desire that interface.
  • Supports functional programming (FP). For example, Go supports anonymous functions, closures, and first-class functions.

Concurrent

  • Built-in concurrency. There are no heavy threads but channels.
  • Ability to program and structure your programs in a synchronous manner but actually it’s asynchronous. Channels hide that complexity and let you structure your programs in a more maintainable way.

Standard Library

  • Almost all of the things built into its standard library (which is the library that comes with Go by default) like HTTP fetching, JSON parsing, and encryption. So, this makes you faster and prevents fragmentation in the ecosystem (most of the time).

Tooling

  • Great built-in command-line tools. Auto-formatting your code, checking race-condition issues, auto-documentation, test coverage reporting, refactoring tools etc.
  • Example Tool: go fmt automatically rearranges your code for you after each save.
1*MyAldQsErzQdOBwRjeWl-w.png?q=20
about-go-language-an-overview-f0bee143597c
Before `go fmt`
1*rZg41FwIp4k8Gzc0_n7UAg.png?q=20
about-go-language-an-overview-f0bee143597c
After `go fmt`
  • Example Tool: go lint makes suggestions to improve your Go code.
1*LyqDApVdBaffqc4pdV-JeA.png?q=20
about-go-language-an-overview-f0bee143597c
$ go lint nocomment.go
nocomment.go:3:1: comment on exported function NoComment should be of the form "NoComment ..."

Some disadvantages of using Go:

  • No generics support. Actually, I’m not counting this as a disadvantage although I put it here. Because it lets you create a very explicit code. Too much wrong abstraction comes with a cost of difficult understandability. Generics are good to some extent however they’re very open to misuse and I’ve seen this in action for years. So, I’m not in the camp of generics support in Go. However, the Go team is still considering adding generics support to the language.
  • About generics (kind of), there’s a great article from Shawn McGrath about the beauty of Doom’s (the game) source code. Especially, read the part: “Minimal Templates”, I couldn’t agree more.
  • Err everywhere. You need to check errors for each of the error producing function in your code explicitly. However, I love the explicitness of Go programs. In the upcoming years maybe we’d find an elegant solution for this. There are some proposals to change error handling.
res, err := http.Client.Get("http://ip.jsontest.com/")// there are no try-catch exceptions in Go, check errors explicitly
if err != nil {
return err
}// ...
  • No function overloading support. However, it can be imitated to some extent by type assertions.
  • Strict rules. Sometimes an advantage, sometimes a disadvantage. For example: Can feel little heavy when you have ever-changing structs. However, most of these rules can be overridden by using Go’s reflection capabilities.
  • Not so strong runtime safety. However, Go doesn’t bring the level of safety, for example, Rust provides. Go’s safety is for compile-time only and only to some extent for runtime (for example forbidden pointer arithmetic). So, if you’re looking also for the runtime safety, look to Rust. Go cares more about the productivity of programmers. Go = fast to production, Rust = safety in production.
  • Smaller number of packages as compared to other ecosystems like Node.js and Ruby. This is because Go’s standard library is full of features which mostly doesn’t need third-party support. However, the number of packages are increasing.
1*RcCAA1eRth9HLFIh9raB4Q.png?q=20
about-go-language-an-overview-f0bee143597c
Go packages
1*558LSzVscsiN2k0NaWaMvA.png?q=20
about-go-language-an-overview-f0bee143597c
Ruby and Nodejs packages

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK