Structs & InterfacesBeginner
1Question 1 of 1
goHow do Structs and implicit Interfaces work in Go?
Explanation & Concept
Go does not have a `class` keyword or traditional inheritance. Instead, it uses **Structs** for data encapsulation and **Composition** (embedding).
Interfaces in Go are satisfied **implicitly**: a type implements an interface simply by implementing its method signatures; there is no `implements` keyword.
Code Example
go
package main
import "fmt"
type Greeter interface {
Greet() string
}
type User struct {
Name string
}
// User automatically satisfies Greeter without explicit declaration
func (u User) Greet() string {
return "Hello, my name is " + u.Name
}
func PrintGreeting(g Greeter) {
fmt.Println(g.Greet())
}
func main() {
u := User{Name: "Sam"}
PrintGreeting(u) // Works seamlessly!
}How It Works & Key Takeaways
Implicit interfaces decouple package implementations. Consumers can define small interfaces matching only the behavior they need.
Mock Test · 1 Questions · Optional
Ready for Error Handling?
Take a quick test to evaluate yourself, or move straight to the next module.
Beginner1 questions