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
Golang HTTP GET request with parameters
Various examples of Carbon date-time package in Golang
Copy an array by value and reference into another array
Regular expression to validate common Credit Card Numbers
How to set timeout for http.Get() requests in Golang?
Catch values from Goroutines
Regular expression to validate the date format in "dd/mm/yyyy"
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time
How to get the current date and time with timestamp in local and other timezones ?
Example to compare Println vs Printf