Go programming for .NET Devs — Intro

Ale Miralles
amiralles
Published in
4 min readMar 20, 2019

I’m going to take a hiatus from algorithms and data structures in Ruby to write about my experiences while learning Go after more than a decade of .NET.

If you are an experienced .NET developer who happens to need to learn Go in no time, this guide might help you.

By focusing on the shortlist of stuff that you don’t know about Go and leaving the rest to your “muscle memory,” this guide might be able to halve the time that it takes to learn Go from scratch.

Let’s start with a quick example to see what I mean.

On C-Style languages, when you have to return a value or assign a variable based on a simple condition, it’s a common idiom to use a ternary expression:

return n % 2 == 0 ? "even" : "odd";

The previous code works in C, C#, JS, and in most C-inspired programming languages; but it doesn’t work in Go. The Go alternative to the previous expression looks something like this:

if m := n % 2; m == 0 {
return "even"
} else {
return "odd"
}

So much for your muscle memory, right?

This series is about that, the WTFs you will find on the road while learning Go coming from .NET or similar C-Style programming languages.

It sounds interesting? Let’s Go ;)

Hello World!

Following the C tradition, let’s begin our journey with a classic “Hello world!” program. The Code we are going to build is analogous to the good all console app that we use for years to try stuff out in .NET.

For this particular example, I’m going to place the code side by side, so it’s easy to see how instructions on one program correspond with the other.

// This is Go                 | // This is C#
package main |
|
import( | using System;
"fmt" |
) |
|
| class Program {
func main() { | public static void Main() {
fmt.Println("Hello World!") | Console.WriteLine(
| "Hello World!");
} | }
| }

Go programs live inside packages, later on, we are going to discuss what is a package, but for now let’s say that is a container for our go source files, something related to libraries, namespaces, and classes in .NET but not quite the same.

If we want to make an “executable” program in Go, the package name must be main.

On .NET there are no such limitations. The concept of a package does not exist as such, and there are no restrictions for the name of the class that contains the “main” method (program’s entry point). It’s common practice to call it “Program” (or “Main” if you come from a C background), but that’s not required, you can call it whatever you like, and it’ll work just fine. It’s also a good practice to organize the code into namespaces, but as you can see in the program above, also not required.

Dissecting the Code

Both programs need an entry point. Following the C tradition, Go and C# programs have the concept of the “main function.” In the case of Go, there is only one signature for this function, while C# we have a few variants (most inherited from C). In future posts, we will see how both languages allow us to access the command line args in our programs but for now, let’s use the simplest form.

// Go                        |  // C#
func main() { | public static void Main() {
} | }

To print out the message “Hello world” we need to import the “format” package in Go and the “System” namespace in C#.

// Go                        |  // C#
import( | using System;
"fmt" |
) |

To access the “print” function in Go, we must use the imported package “fmt” as if it were a static class in .NET. That is, package name, dot, function name.

In C#, we use the good old “Console”, of course.

// Go                        |  // C#
fmt.Println("Hello World!") | Console.WriteLine("Hello World!");

Notice that there is an essential difference between Go and C#. Since Go doesn’t have the concept of classes, when we import a package we get a pointer to the package that allows us to call functions defined in that package. The same thing does not exist in C#. When we require a namespace, we get access to its classes and using those classes we can call methods.

(The concept of “static imports” has been in C# for years now but it’s not quite the same as the concept of imports in Go).

// C#
using static System.Console;
public static void Main() {
// The next line calls the "WriteLine" method
// on the statically imported "Console" class.
WriteLine("Hello World!");
}

Running the Code

There are several options to run Go programs, but my favorite one is to save the program on disc and run it from there using the go run command.

$ go run program.go

(Notice that the name of the file doesn’t matter; it doesn’t have to match the package name)

To run the .NET program you can use VS, of course. But since I’m a long time mono user, the way I usually go is by running this command:

$ mcs program.cs && mono program.exe

That’s for this brief intro. Next time we will see how to split our go programs into multiple files. Stay tuned!

--

--

Ale Miralles
amiralles

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors.