The Golang Trinity: Functions, Methods, Interfaces
The article discusses the fundamental concepts of functions, methods, and interfaces in the Go programming language. It explains how methods are functions associated with types and how interfaces define required methods for types. The piece emphasizes the utility of functions as interfaces, allowing for flexible programming patterns.
- ▪A method is a function with a receiver, while an interface is a set of method signatures.
- ▪Functions are best used for stateless operations, whereas methods are used when behavior is tied to a type.
- ▪The article illustrates how functions can be treated as interfaces, enabling simpler function usage in contexts expecting interfaces.
Opening excerpt (first ~120 words) tap to expand
try { if(localStorage) { let currentUser = localStorage.getItem('current_user'); if (currentUser) { currentUser = JSON.parse(currentUser); if (currentUser.id === 3732443) { document.getElementById('article-show-container').classList.add('current-user-is-article-author'); } } } } catch (e) { console.error(e); } Kevin Nambubbi Posted on May 28 The Golang Trinity: Functions, Methods, Interfaces #go Function: Does something with inputs Method Does something attached to a type Interface Says what methods a type must have A method is a function with a receiver. An interface is a set of method signatures. If a type has those methods → it satisfies the interface. Automatically. No implements. No inheritance. Just behavior.
…
Excerpt limited to ~120 words for fair-use compliance. The full article is at DEV.to (Top).