This is the source code for How To Build Extensible Go Applications with Plugins
$ git clone https://github.com/ManiMuridi/go-plugins-shipping-calculator.git// Your implemented interface must comply with this interface
type Shipper interface {
Name() string
Currency() string
CalculateRate(weight float32) float32
}package main
type shipper struct {}
func (s shipper) Name() string {
return "United States Postal Service (USPS)"
}
func (s shipper) Currency() string {
return "USD"
}
func (s shipper) CalculateRate(weight float32) float32 {
cost := weight * 1.5
tax := cost * .10
return cost + tax
}
var Shipper shipper$ go build -buildmode=plugin -o ./plugins/usps.so usps/usps.go# Update the Makefile to look like this
.PHONY: usps fedex royalmail
usps:
go build -buildmode=plugin -o ./plugins/usps.so usps/usps.go
fedex:
go build -buildmode=plugin -o ./plugins/fedex.so fedex/fedex.go
royalmail:
go build -buildmode=plugin -o ./plugins/royalmail.so royalmail/royalmail.goThen call the make usps command in your terminal
$ make usps# command: go run main.go {shipping method} {weight}
$ go run main.go usps 5