Go programming for .NET Devs — Intro

Ale Miralles
Mar 20 · 4 min read
return n % 2 == 0 ? "even" : "odd";
if m := n % 2; m == 0 {
return "even"
} else {
return "odd"
}

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.

// 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!");
} | }
| }

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). On 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() {
} | }
// Go                        |  // C#
import( | using System;
"fmt" |
) |
// Go                        |  // C#
fmt.Println("Hello World!") | Console.WriteLine("Hello World!");
// 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
$ mcs program.cs && mono program.exe

amiralles

Concise programming articles for those who code

Ale Miralles

Written by

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

amiralles

amiralles

Concise programming articles for those who code