Example Function that takes an interface type as value and pointer?
Example
package main
import "fmt"
type Showprice interface {
display(int)
}
func display(s Showprice, price int) {
s.display(price)
}
type Car struct {
Price int
}
func (c Car) display(price int) {
c.Price = price
}
type Bike struct {
Price int
}
func (b *Bike) display(price int) {
b.Price = price
}
func main() {
ca := Car{Price:2000}
bi := Bike{Price:400}
display(ca, 3000)
display(&bi, 600)
fmt.Println("Car:",ca.Price)
fmt.Println("Bike:",bi.Price)
}
Output
Car: 2000
Bike: 600
Most Helpful This Week
Catch values from Goroutines
How to read/write from/to file in Golang?
Runtime package variables
Various examples of Carbon date-time package in Golang
Various examples of printing and formatting in Golang
How to check pointer or interface is nil?
How to use a mutex to define critical sections of code and fix race conditions?
How to iterate over an Array using for loop?
How to check string contains uppercase lowercase character in Golang?
How to check lowercase characters in a string in Golang?