-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (43 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
const (
filename = "datajson.json"
)
func main() {
plan, _ := ioutil.ReadFile(filename)
var data interface{}
err := json.Unmarshal(plan, &data)
if err != nil {
fmt.Println(err.Error())
return
}
dataConvString := string(plan)
hargaItem := GetHargaItem(dataConvString, "voucher10", "voucher", "Contoh Voucher")
fmt.Println("Harga perItem: ", hargaItem)
}
func GetHargaItem(dataArray, productCode, productType, productDesc string) float64 {
productDescReplace := strings.ReplaceAll(productDesc, " ", "_")
fmt.Println("productType: ", productType)
fmt.Println("productDescReplace: ", productDescReplace)
var dataMap map[string]interface{}
json.Unmarshal([]byte(dataArray), &dataMap)
dataMap2 := dataMap["data"].(map[string]interface{})
var totalHarga float64
feeTransaction := 500.0
for _, item := range dataMap2["pricelist"].([]interface{}) {
itemMap := item.(map[string]interface{})
if itemMap["product_code"] != nil && itemMap["product_code"].(string) != "" {
if strings.ToLower(itemMap["product_code"].(string)) == strings.ToLower(productCode) {
totalHarga += itemMap["product_price"].(float64) + feeTransaction
} else {
continue
}
}
}
return totalHarga
}