I’m a Software Engineer with some years of experience in C#, Java, JavaScript and tiny bit of other languages. For fun (I don’t yet need this at work) I spent one day going through GoLang tutorials, this video, and skimming through “The Go Programming Language” book. This blog post is a collection of things in Go that I found to be interesting and worth mentioning. In NO way this can be considered a tutorial or reliable source of information on the language. The best and most comprehensive place to learn about the language is here.
So there you go, golang in 20+ bullet-points:
There is shadowing for variables (package level variables can be overshadowed by local scope variables).
Package level variables can be exported just based on convention of whether they start with a capital.
complex type is a first class citizen type in golang much like int and string.
There is a type called “Rune” which is nothing more than Int32 alias to represent characters
iota is weird way of incrementing inside of const blocks resetting itself in a new constant block
arrays are completely copied on variable assignment b := a, if you want to reference same array you can do b := &a
slices are variable length reference arrays
there is spread syntax with three dots ... like in some other languages and/or frameworks
removing elements from a slice is a funny business requiring you to cut slice in two pieces not containing target element and then concatenating those pieces together
To define a map you write something like someMap := map[int]string{1: "one", 2: "two"} and then you get retrieve from it with indication if key exists getOne, ok := someMap[1]
You can break out of a parent loop from within nested loop by applying labels to loops.
There are no classes or inheritance as such. Closest things to this are struct and composition via process called embedding
Execution of code can be deferred. This is quite interesting, as executions is scheduled to the end of function block and if there are multiple defer statements they are unwind in FILO mode, even if there was exception thrown panic.
Yeah, there are no exceptions in golang, or better to say, they are not called exceptions but rather panic. Panic can be controlled by recover() function. Probably easiest way to think about this are throw and catch with a caveat that error thrown is not exactly same idiomatically as in other languages.
Go is similar to C/C++ as it has pointers, de-referencing, function parameters can be sent by value or reference. Go does not allow pointers arithmetic unless you are using unsafe.
Functions support named return values and multiple return values. This is really nice in my opinion.
Functions are a bit similar to functions in javascript from usage perspective. They can be immediately invoked and passed around as a param.
Interfaces can be applied on any type where you can add methods; they can be composed (just like structs); Go seems to be doing a lot of emphasis on interface segregation.
Goroutines combined with channels is probably something most interesting and unique about the language.
Goroutine is a thread abstraction, sometimes called “green thread”. They are cheap to create and easy to control. They do not span OS level threads (unless they must, I guess). The way to asynchronously call something is just go getMeADrink()
Goroutines can be controlled using waitgroup and mutex. This is standard shared memory multithreading model on very high level similar to other languages.
Channels is a way to pass data between goroutines. It can either be a single type or a buffer of things. This is when multithreading can be implemented much differently from other languages. This is also called communicating sequential processes model of multithreading.
I hope this was useful to some of you. For me personally this day gave me some idea what the language is about and the syntax no longer looks alien. Next thing to do would probably be to spend one more day playing with goroutines/channels specifically and actually reading the book for more fun.
This website uses cookies. We'll assume you're ok with this, but you can opt-out if you wish.AcceptRead More
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
code
more code
~~~~