Skip to content

Instantly share code, notes, and snippets.

@wuriyanto48
Created August 7, 2017 08:08
Show Gist options
  • Save wuriyanto48/97b510d4bcc21498d7b1838eb4df8c91 to your computer and use it in GitHub Desktop.
Save wuriyanto48/97b510d4bcc21498d7b1838eb4df8c91 to your computer and use it in GitHub Desktop.
FInd Highest Value inside Go Struct's Field
package main
import (
"fmt"
"encoding/json"
)
type ShippingCostReadModel struct {
TypeId string `json:"typeId,omitempty"`
TypeName string `json:"typeName,omitempty"`
DestinationId string `json:"destinationId,omitempty"`
Destination string `json:"destination,omitempty"`
ZipCode string `json:"zipCode,omitempty"`
WebCost int `json:"webCost,omitempty"`
KGP int `json:"KGP,omitempty"`
KGS int `json:"KGS,omitempty"`
EstimatedDay string `json:"estimatedDay,omitempty"`
}
type ShippingCosts []ShippingCostReadModel
func (costs ShippingCosts) FindHighestCost() ShippingCostReadModel {
var biggest,n ShippingCostReadModel
for _,v:=range costs {
if n.WebCost < v.WebCost{
n = v
biggest = n
}
}
return biggest
}
var shippingDatas = []byte(` [
{
"typeId": "EXP0101",
"typeName": "TIKI-REG",
"destinationId": "",
"destination": "Tarakan Barat",
"zipCode": "77111",
"webCost": 40000,
"KGP": 40000,
"KGS": 40000,
"estimatedDay": "2"
},
{
"typeId": "EXP0101",
"typeName": "TIKI-REG",
"destinationId": "",
"destination": "Samarinda Ulu",
"zipCode": "77111",
"webCost": 40000,
"KGP": 40000,
"KGS": 40000,
"estimatedDay": "2"
},
{
"typeId": "EXP1901",
"typeName": "JNE-REG",
"destinationId": "TRK10011",
"destination": "Tarakan Barat",
"zipCode": "77111",
"webCost": 54000,
"KGP": 54000,
"KGS": 54000,
"estimatedDay": "3"
},
{
"typeId": "EXP1901",
"typeName": "JNE-REG",
"destinationId": "SMD10010",
"destination": "Samarinda Ulu",
"zipCode": "77111",
"webCost": 46000,
"KGP": 46000,
"KGS": 46000,
"estimatedDay": "3"
}
]`)
func main() {
var shippings ShippingCosts
_ = json.Unmarshal(shippingDatas, &shippings)
fmt.Println(shippings.FindHighestCost())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment