Interface #
Interface dalam bahasa pemrograman Golang adalah tipe yang mengabstraksi kumpulan metode tanpa harus mengimplementasikan logika spesifik. Interface memungkinkan kita untuk mendefinisikan perilaku yang harus dipenuhi oleh tipe-tipe konkret, sehingga membantu dalam menciptakan kode yang lebih fleksibel dan dapat diperluas.
Berikut adalah penjelasan tentang penggunaan interface dalam Golang beserta contohnya:
Mendefinisikan Interface #
Interface didefinisikan menggunakan kata kunci type
diikuti dengan nama interface dan kata kunci interface
.
package main
import "fmt"
// Mendefinisikan interface
type Describer interface {
Describe() string
}
Mengimplementasikan Interface #
Tipe konkret (struct) mengimplementasikan interface dengan menyediakan definisi untuk semua metode yang ada dalam interface tersebut.
package main
import "fmt"
// Mendefinisikan interface
type Describer interface {
Describe() string
}
// Mendefinisikan struct
type Person struct {
Name string
Age int
}
// Mengimplementasikan metode Describe pada struct Person
func (p Person) Describe() string {
return fmt.Sprintf("My name is %s and I am %d years old.", p.Name, p.Age)
}
func main() {
var d Describer
person1 := Person{Name: "Alice", Age: 30}
d = person1
fmt.Println(d.Describe()) // Output: My name is Alice and I am 30 years old.
}
Interface Kosong #
Interface kosong (empty interface) adalah interface yang tidak memiliki metode dan dapat menampung nilai dari tipe apapun.
package main
import "fmt"
func describe(i interface{}) {
fmt.Printf("Value: %v, Type: %T\n", i, i)
}
func main() {
describe(42)
describe("Hello")
describe(true)
}
Menggunakan Interface untuk Fungsi Polimorfik #
Interface memungkinkan Anda untuk menulis fungsi yang dapat bekerja dengan berbagai tipe yang berbeda selama tipe tersebut mengimplementasikan interface.
package main
import "fmt"
// Mendefinisikan interface
type Describer interface {
Describe() string
}
// Mendefinisikan struct
type Person struct {
Name string
Age int
}
// Mengimplementasikan metode Describe pada struct Person
func (p Person) Describe() string {
return fmt.Sprintf("My name is %s and I am %d years old.", p.Name, p.Age)
}
// Mendefinisikan struct lain
type Product struct {
Name string
Price float64
}
// Mengimplementasikan metode Describe pada struct Product
func (p Product) Describe() string {
return fmt.Sprintf("Product name: %s, Price: $%.2f", p.Name, p.Price)
}
// Fungsi yang menerima parameter interface
func printDescription(d Describer) {
fmt.Println(d.Describe())
}
func main() {
person1 := Person{Name: "Alice", Age: 30}
product1 := Product{Name: "Laptop", Price: 999.99}
printDescription(person1)
printDescription(product1)
}
Type Assertion #
Type assertion digunakan untuk mengambil nilai konkret dari interface kosong.
package main
import "fmt"
func main() {
var i interface{} = "Hello"
// Type assertion
str, ok := i.(string)
if ok {
fmt.Println(str) // Output: Hello
} else {
fmt.Println("Type assertion failed")
}
}
Type Switch #
Type switch digunakan untuk menangani beberapa kemungkinan tipe dalam interface kosong.
package main
import "fmt"
func describe(i interface{}) {
switch v := i.(type) {
case string:
fmt.Printf("I am a string: %s\n", v)
case int:
fmt.Printf("I am an integer: %d\n", v)
default:
fmt.Printf("I am of type %T\n", v)
}
}
func main() {
describe(42)
describe("Hello")
describe(true)
}
Contoh Lengkap Penggunaan Interface #
package main
import "fmt"
// Mendefinisikan interface
type Describer interface {
Describe() string
}
// Mendefinisikan struct
type Person struct {
Name string
Age int
}
// Mengimplementasikan metode Describe pada struct Person
func (p Person) Describe() string {
return fmt.Sprintf("My name is %s and I am %d years old.", p.Name, p.Age)
}
// Mendefinisikan struct lain
type Product struct {
Name string
Price float64
}
// Mengimplementasikan metode Describe pada struct Product
func (p Product) Describe() string {
return fmt.Sprintf("Product name: %s, Price: $%.2f", p.Name, p.Price)
}
// Fungsi yang menerima parameter interface
func printDescription(d Describer) {
fmt.Println(d.Describe())
}
func main() {
person1 := Person{Name: "Alice", Age: 30}
product1 := Product{Name: "Laptop", Price: 999.99}
printDescription(person1)
printDescription(product1)
// Menggunakan interface kosong
var empty interface{}
empty = 42
fmt.Println(empty) // Output: 42
empty = "Hello"
fmt.Println(empty) // Output: Hello
// Type assertion
if str, ok := empty.(string); ok {
fmt.Println(str) // Output: Hello
} else {
fmt.Println("Type assertion failed")
}
// Type switch
describe := func(i interface{}) {
switch v := i.(type) {
case string:
fmt.Printf("I am a string: %s\n", v)
case int:
fmt.Printf("I am an integer: %d\n", v)
default:
fmt.Printf("I am of type %T\n", v)
}
}
describe(42)
describe("Hello")
describe(true)
}
Kesimpulan #
Dengan menggunakan interface, Anda dapat membuat kode yang lebih fleksibel dan dapat diperluas, memungkinkan berbagai tipe untuk bekerja bersama dalam cara yang seragam selama mereka mengimplementasikan metode yang ditentukan oleh interface.