/go-for-csharp-devs

Links, questions, and experiments gathered along my way learning Go.

Primary LanguageGo

Go for C# developers

Learning GO is not only about a new language, but also about a different philosophy from what we are used to.

This page contains some useful links and answers to questions I had a long the way. Hopefully, it can help you too.

NB! This is and always will be a work in progress!

I'll keep adding more materials, details, and answers a long the way, but as anything in life, the learning process is endless, therefore, the material in here can become outdated.

Table of contents

Materials

Official docs

  • Effective Go - https://go.dev/doc/effective_go
    • A document that gives tips for writing clear, idiomatic Go code. A must-read for any new Go programmer. It augments the tour and the language specification, both of which should be read first.
  • Go Code Review Comments https://github.com/golang/go/wiki/CodeReviewComment
    • This page collects common comments made during reviews of Go code, so that a single detailed explanation can be referred to by shorthands. This is a laundry list of common mistakes, not a comprehensive style guide. You can view this as a supplement to Effective Go.
  • Go Docs - https://go.dev/doc/
    • Root for many useful documentations
  • FAQ - https://go.dev/doc/faq
    • Answers to common questions about Go.

Blogs

Courses

  • The way to go - https://www.educative.io/courses/the-way-to-go

    • It's a course from educative.io, it goes from the basics concepts of the language to more advanced ones. It also brings very interesting insights into the differences between the approaches of Java/C# to Go.
    • It has many challenges in exercises you can do directly in the browser. I found it excellent because I didn't need to have any thing installed and I managed to do it in anything that has a browser.
  • How to code in go - https://www.digitalocean.com/community/tutorial_series/how-to-code-in-go

    • A great collection of tutorials that cover basic Go concepts, ideal for beginers

Videos

Podcasts

  • Go Time - Spotify
    • Diverse discussions from around the Go and its community

Code

Playground

If you want to try out something directly in your browser you can use The Go Playground.

Experiments

The folder experiments will contain pieces of code I'm playing around, and also examples related to the questions below.

FAQ for C# developers

1 - For what should I use Go?

Go has several uses, since command line applications, web services (yes web apis too :) ), and container applications like Docker and Kubernetes.

2 - Which text-editor/IDE should I use?

If you are coming from Visual Studio/Rider background, you will feel more familiar with Visual Studio Code and GoLand, however there are others IDEs/text editors out there with nice tooling, like Emacs.

  • Visual Studio Code has everything you need to develop your application, and it's free. It's one of the best text editors, the debugger experience is decent, and there are really good plugins to help out with producitivity. The extension developed by Google itself is a must have.

  • GoLand it's a more robust IDE built by JetBrains. If you familiar with Rider or IntelliJ IDEA you will fell at home. It's not free, but its price is affordable.

3 - Does Go compile to some sort of Intermediate Language?

Nope. There is no IL, the compiler generates an executable that can be executed directly by the computer.

4 - Is it possible to write async code which won't block the main thread while it's awaiting for something?

Huge yes! In Go we don't deal with threads directly. Instead, we use something called goroutines. In some sense they are similar to a Task which is not a necessary a thread but represents some work to be done, which will be eventually scheduled, executed in a thread, and resumed.

I'm not naive to try to explain it in a short answer, so check this video from Rob Pike about it. This video about concurrency patterns explains how concurrency works and some "patterns" like multiplexing, generator, fan-in, etc.

5 - Do I need to use goroutines and channels to make non blocking IO calls?

No, it's not needed. Go offers non-blocking io calls via an blocking interface. It means that despite you are calling a "sync" method, under the hood it will use goroutines to interact with async APIs from the OS. The answers for this question on Stackoverflow give more details about how it's accomplished.

6 - Where are the classes?

Go doesn't have the concept of class. It has type struct which you can define functions to it creating methods. e.g.

package main

import "fmt"

type person struct {
    age int
    name string
}

func (p *person) sayMayName() string {
	return p.name;
}

func main() {
	var m multiplier
	m = &multiplyByFour{}
	fmt.Println(m.multiply(2))
}

7 - What about interfaces?

An interface type in Go are similar to an interface in C#, with the following differences:

Another difference is how to implement an interface. A type that implements an interface doesn't need to know it. As long as it has all the methods defined in the interface, you will be able to pass it. e.g.

package main

import "fmt"

type multiplier interface {
	multiply(val int) int
}

type multiplyByFour struct {
}

func (m *multiplyByFour) multiply(val int) int {
	return val * 4
}

func main() {
	var m multiplier
	m = &multiplyByFour{}
	fmt.Println(m.multiply(2))
}

Note the type multiplyByFour doesn't have a : multiplier or implements multiplier. It just have all methods defined in the interface

8 - What about Nullable objects?

Go basic types have defined zeroed values, and the same also applies for structs.

If you need to differentiate between nil and a zeroed value, you need to create a pointer for that type. e.g.

var person *Person
fmt.Println(person == nil) // prints true

Some types are nullable (a.k.a. have their zeroed value as nil) as standard, e.g. slices and maps

9 - Is there anything similar to LINQ?

There is nothing like LINQ built in the standard library. However, there are some libraries out there, like go-linq. However, such a library is not something I see used often.

10 - How should I organize my project?

Sorry, but there is no easy answer for that, there are infinite ways to organize your project. Like everything in software development, it depends and it's important to understand the drawbacks of every decision. Therefore, until you have a good understanding of how your project will evolve, try to defer decisions as much as possible. You will only know the best way to organize your project after it has some code in it.

This presentation from Kat Zien discusses some patterns and trends (at least from 2018). Use it as a way to get to know options and their pros and cons.

On the other hand, I really like the advice given by Dave Cheney in his presentation about Real world advice for writing maintainable Go programs, especially in the section about Project Structure. It has several valuable tips and recommendations.

Coming from a C# development background, the big challenge is to resist the urge to split things (too early), creating too many packages and files.

11 - Where is the decimal type?

Go doesn't have a primitive decimal type for arbitrary-precision fixed-point decimal numbers. Yes, you read it right. Therefore, if you need to deal with fixed-point precision there are two main options:

  • Use an external package like decimal, which introduces the decimal type. However, the current version (1.3.1), can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
  • Use int64 to store and deal with these numbers. For e.g. given you need 6 precision digits, therefore 79.23, 23.00, and 54.123456, become respectively 79230000, 23000000, and 54123456.

Next Questions:

  • What about dependency injection containers?
  • Which framework should I use for writing web APIs?
  • What is the preferred t way to write logs?