hi@methebe.com

Go Interfaces — You Think JavaScript is Flexible? - 22/12/2023

Forget everything you know about Object-Oriented Programming. Golang embraces structural typing!

Site under construction! Migrated content might be displayed ugly!

medium-to-markdown@0.0.3 convert node index.js https://medium.com/stackademic/go-interfaces-you-think-javascript-is-flexible-0d907b10f75e

Go Interfaces — You Think JavaScript is Flexible?

[Enigma Bits

](https://medium.com/@birnadin?source=post_page-----0d907b10f75e--------------------------------)[![Stackademic](https://miro.medium.com/v2/resize:fill:48:48/1*U-kjsW7IZUobnoy1gAp1UQ.png)

](https://blog.stackademic.com/?source=post_page-----0d907b10f75e--------------------------------)

Enigma Bits

·

Follow

Published in[

Stackademic

](https://blog.stackademic.com/?source=post_page-----0d907b10f75e--------------------------------)·4 min read·Dec 22, 2023

Listen

Share

interfaces we interact with. (from unsplash.com)

Web development is an ever-evolving field, often brimming with new technologies and methodologies. Today, we’re diving into the world of Golang, particularly its unique approach to interfaces.

Buckle up, because we’re not just here to tickle your brain cells but also to challenge some traditional concepts. And yes, there might be a subtle sarcastic joke or two, so keep your wits about you.

Code examples can be found in this GitHub gist of mine.

Golang: A Break from Tradition

Forget everything you know about Object-Oriented Programming (OOP) for a moment. Golang takes a different route, embracing structural typing and composition over OOP’s inheritance and polymorphism.

If it quacks and walks like a duck, Golang believes it’s a duck.

Interfaces. These are the core language constructs in Golang, stepping in to subsidize the role of inheritance in traditional OOP.

Golang Interface

In case you have been wondering, here are some pros relative to conventional OOP paradigm (Go is procedural, by the way!)

Consider this example:

an implementation.

Here, `demoFunction` can accept anything that implements `dummyMethod` with the right signature. It’s like saying, “I don’t care what you are, just do this thing.”

an exemplary code sample.

In Golang, interfaces with structural typing effectively eliminate the need for generics and function overloading. Plus, you get to name your interface parameters whatever suits the context — talk about freedom!

Multi-function Interfaces. Interfaces can be more than a one-trick pony, containing multiple function definitions as per your desire.

In the Go universe, interfaces are like Java’s Abstract Classes, but with less baggage.

Hey, if you’re finding this insightful, consider subscribing to my newsletter, so you won’t miss the next gem.

you are enjoying Enigma Bits

Beyond Basics

Empty Interfaces

The empty `interface{}` can hold anything. Yes, anything. They’re perfect for when you need to juggle diverse data structures or implement ultra-flexible functions.

Type Assertions

This method is utilized to extract an interface’s underlying value, enabling access to its concrete type.

The syntax, `value, ok := interfaceVar.(Type)`, checks if `interfaceVar` is of a specific `Type`.

This feature is crucial for leveraging Go’s dynamic interface capabilities effectively.

Type Switch

Type switch in Go is a construct for comparing an interface’s type with multiple types, similar to a switch statement but focusing on types rather than values. This allows for flexible, type-safe handling of different data types in a clean and efficient manner.

Real World Examples

Let’s see how this plays out in a real-world scenario, like handling JSON data of varying structures.

Please refer the `type_switch.go` in the gist. Since these are long, I avoid placing them here.

Here, `processJSON` is a chameleon, smoothly handling both single objects and arrays of objects. It’s a testament to Golang’s flexibility and its prowess in handling dynamic data formats.

Think statically typed languages are a roadblock? Think again. Maybe it’s not the language, but how you’re using it. 😜

And there you have it, a journey through Golang interfaces, from the basics to real-world applications. Remember, whether you’re a beginner or a seasoned veteran, there’s always more to learn and explore in web development.

If you enjoyed this, don’t forget to subscribe and support my college journey. Happy coding!

See you on Golang’s answer to OOP lovers, my next article continuous to this. If you don’t want to miss it, subscribe; till then it’s meTheBE signing off 👋.

Stackademic

Thank you for reading until the end. Before you go: