DEV Community

Cover image for How I Stopped Worrying and Learned to Love Go Interfaces

How I Stopped Worrying and Learned to Love Go Interfaces

Allan Githaiga on October 23, 2024

Hey there, fellow Go newbies (or should I say Gophers-in-training🕧)! 🌱 Interfaces seemed like one of those mysterious, magical things that everyone...
Collapse
 
martinbaun profile image
Martin Baun

Go makes me love my job :)

Collapse
 
githaiga22 profile image
Allan Githaiga

Hey martin..indeed Go is the best language so far to work with especially while focusing on the back-end side..Looking foward to collaborate with you in any kind of projects you need assistance
reach me out thru my email : allangithaiga5@gmail.com
Best regards,
Allan Githaiga

Collapse
 
dyfet profile image
David Sugar

This is true for services development in general, not just web backends, and those issues I used to find vexing (vendor caching, for offline builds, for example) are now long gone. It remains clean to read with long term stability in the compiler and ecosystem, so you don't have things constantly change and break every time you happen to do a newer build...

Collapse
 
yesnik profile image
Nikita N

This article is one of the most useful I've read recently about GoLang, especially regarding interfaces. Thank you!

Collapse
 
shogg_realr_88d77b7301db6 profile image
Info Comment hidden by post author - thread only accessible via permalink
Shogg Realr

this article was one of the worst

Collapse
 
mi113r profile image
Mi113r • Edited

All articles at this level, don't answer the question
“Why not use the original structure ?”

 shape = Rectangle{Width: 5, Height: 4}
  fmt.Println("Rectangle Area:", shape.Area())           // Outputs: 20
  fmt.Println("Rectangle Perimeter:", shape.Perimeter()) // Outputs: 18

 // Ok, its similar ?
 rectangle := Rectangle{Width: 5, Height: 4}
    fmt.Println("Rectangle Area:", rectangle .Area())           // Outputs: 20
    fmt.Println("Rectangle Perimeter:", rectangle .Perimeter()) // Outputs: 18

// why i need use interface ?
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sasho_ristevski_374c5bc1c profile image
Sasho Ristevski

If you don't use an interface, you'd end up duplicating a lot of the same methods (like Area() and Perimeter()) across different structs. The benefit of the interface comes when you need to handle a diverse range of shapes without constantly worrying about the details of each one.

Collapse
 
pallavigodse profile image
Pallavi Godse

I too worry too much when working in Go. Thank you for this guide!

Collapse
 
trapper99 profile image
Stanley Kariuki

If I may ask how do you make your blog banners especially the Go character?

Collapse
 
githaiga22 profile image
Allan Githaiga

well i just modify them using UX tools like figma,miro or canva

Collapse
 
fredgitonga profile image
Fred

great article @githaiga22 , this is a nice and fun way to explain interfaces.

Collapse
 
denilany profile image
Denil

Nice article, straight to the point.

Collapse
 
lamine_fall profile image
Lamine

Salut à tous.
Je suis développeur d'application Android et je viens juste de terminer mon application gratuite de philosophie.
J'aimerais avoir des testeurs pour m'aider à l'améliorer. Voici mon adresse mail :
Laminefalldeveloppeur@gmail.com

Collapse
 
victorpaularony profile image
Smally Pauls

nice learning interface and struct diffrances

Collapse
 
kurealnum profile image
Oscar

Just a heads up that you can add highlighting to the code blocks if you'd like. Just change:

code block with no colors example

... to specify the language:

code block with colors example

More details in our editor guide!

Collapse
 
sasho_ristevski_374c5bc1c profile image
Sasho Ristevski

for the full explanation of the example above i find this useful
youtube.com/watch?v=SX1gT5A9H-U

Collapse
 
prakash_chokalingam profile image
prakash chokalingam

Great article! Thanks for the explanation.

Collapse
 
shogg_realr_88d77b7301db6 profile image
Info Comment hidden by post author - thread only accessible via permalink
Shogg Realr • Edited

Yep, you don't understand anything about Go interfaces. An interface is a type. You never used the interface you declared. You just called methods all named the same (Sound). And then you promoted the empty interface which is a really bad advice.

Collapse
 
shogg_realr_88d77b7301db6 profile image
Info Comment hidden by post author - thread only accessible via permalink
Shogg Realr • Edited

To add more context, fmt.Println accepts variadic 'any'. This code is not generic or templated. It has nothing to do with interfaces. It relies on Runtime Reflection, which is the slowest. I use it in my libs.

Some comments have been hidden by the post's author - find out more