What's the coolest feature of Go?

It seems like my only posts here involve asking for help so, for the sake of not being a leech, I’ll ask this: What is your favorite feature of Go?

Go HTML Templates. Working with Drupal and Wordpress for a couple of decades, it is refreshing to find a super simple way to create static web sites that is really fast…

For me, it’s that there’s no difference between synchronous and asynchronous functions.

I also read and write a lot of C# and a little Python code and you have to know when you’re in the middle of an async or non-async call stack. Not that it’s hard; it’s easy to notice the async keyword in the function definition, returning a Task/ValueTask, seeing awaits everywhere, etc., but you have to know to change what you’re doing based on context. Also if you didn’t actually need any asynchronous code, but you’re implementing an interface, you need to wrap your result in a Task.FromResult which results in unnecessary heap allocations. Of course, this is less of an issue since the introduction of ValueTasks, but I have a C# project that builds a DLL that is loaded as a plugin by a 3rd party application that doesn’t let me reference my own 3rd party DLLs (so I cannot use System.Threading.Tasks.Extensions to get ValueTask in the .NET Framework).

Go makes all of that go away: You don’t change anything based on functions being asynchronous or synchronous; they’re just functions. If your function calls some blocking operation, the goroutine is parked while that operation completes and some other goroutine can run in the meantime without any explicit asyncs or awaits.

5 Likes

For me, Go’s simple concurrency model :slight_smile:

5 Likes

Adding to the excellent things people have already mentioned, I think one of the coolest features of Go is how capable the stdlib is. I am continually impressed with how far I can easily go on various projects I’m building without adding a single external dependency. For example, I needed to build a reverse proxy recently and, wouldn’t you know it, there’s an easy stdlib way to do it.

1 Like

Thanks for starting this thread @ashinnv! Along with others’ mentions of concurrency and @Dean_Davidson’s praise for the stdlib, I’ll add the feature of built-in file trees using embed. I recently made my first web app and having the site built into the cross (linux) platform, compiled executable is SO amazing! Sure I could cobble something together in another language, and maybe other languages do this, but I’m not aware of it. It’s just so easy and natural with Go!

3 Likes

This was one of the first things that I noticed when I started using Go. I say “noticed” but it was more like the “WHAT IS WRONG WITH YOU???” that I got for doing string substitutions in the place of HTML templates.

I’d have to say that I’m pretty much there with you. Goroutines combined with channels makes me more happy than it should.

1 Like

Gonna have to admit; I’ve never noticed this before, but now that’s an issue because I’m already thinking of excuses to implement it.

That looks very interesting and I wasn’t aware of it. It looks like I could embed all of my html templates into the build. Thanks

My favorite feature is how easy it is to set up a web server as I had never tried it before last November. Also the speed of the execution and the speed of the build are very impressive. Also, the support from the forum has been great.

1 Like

Yes, @rschluet! That’s exactly what I did with it. 1.16 was released just as I was designing my first web-enabled app and it seemed a natural fit for what I’m doing (HTTP-based configuration for the app). I embedded the templates, CSS, JS, etc. and also included a CLI option (–dev-mode) to use the local file system while developing the web parts of the app. Here are the declarations:

//go:embed site
var files embed.FS

// Do not embed these, use local file system.
var localFiles fs.FS

And here’s how I use it:

func handleRequests() {
	var fileSystem fs.FS
	var fileServer http.Handler
	. . .
	if cmdline.DevMode {
		if _, err := os.Stat(cmdline.DevSite); errors.Is(err, os.ErrNotExist) {
			log.Printf("Could not find directory '%s' (%v). Using *embedded* website.\n", cmdline.DevSite, err)
			fileSystem = files
		} else {
			log.Printf("Using *local* directory %s for the website.\n", cmdline.DevSite))
			localFiles = os.DirFS(cmdline.DevSite)
			fileSystem = localFiles
		}
	} else {
		log.Println("Production mode; using *embedded* website.")
		fileSystem = files
	}
	fileServer = http.FileServer(http.FS(fileSystem))
	. . .
}

The coolest feature of Go is that there are very few features.

1 Like

For me : the fast compile time, easy to learn syntax, great performance, easy concurrency logic, great open source packages