Skip to content

ManiMuridi/go-plugins-shipping-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Plugins Example: Shipping Calculator

This is the source code for How To Build Extensible Go Applications with Plugins

Clone this project

$ git clone https://github.com/ManiMuridi/go-plugins-shipping-calculator.git

Plugin Interface

// Your implemented interface must comply with this interface
type Shipper interface {
	Name() string
	Currency() string
	CalculateRate(weight float32) float32
}

Add New Plugin Implementation

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

Build the Plugin Manually

$ go build -buildmode=plugin -o ./plugins/usps.so usps/usps.go

Build the Using the Makefile

# 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.go

Then call the make usps command in your terminal

$ make usps

Run the app

# command: go run main.go {shipping method} {weight}
$ go run main.go usps 5

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published