如何使用Go

时间:2019-03-04 06:57:33

标签: go

我正在开发一个网站,后端使用Go,前端使用Angular。 在Go中,我从数据库中获取原始数据并引用固定设置表(JSON格式),然后覆盖相应的列。

原始数据如下:

 Site  Code           Main
       0700-Shift     010_A
       2135-Packing   030_C
       3343-Check     050_E
       4355-Casting   080_H
       6903-ReDo Test 020_B
       2277-Scope chk 040_D

我切掉了餐桌的一部分:

[{"010_A": [
{
  "Code1": "010_01",
  "Code2": "",
  "Seq": "000 Start",
},
{
  "Code1": "010_07",
  "Code2": "010_0700",
  "Seq": "010 Shift"
},
],
"020_B": [{
  "Code1": "020_69",
  "Code2": "",
  "Seq": "000 ReDo Test"
},
{
  "Code1": "020_27",
  "Code2": "",
  "Seq": "000 Redo Combine"
}
],
"080_H": [
{
  "Code1": "080_06",
  "Code2": "",
  "Seq": "005 Merge"
},
{
  "Code1": "080_43",
  "Code2": "",
  "Seq": "010 Casting"
},
{
  "Code1": "080_66",
  "Code2": "080_6621",
  "Seq": "100 Cooling"
}
]}]

用于设置表的Go结构为:

type Settingtable struct {
    Code1    string
    Code2    string
    Seq      string
}

我首先使用“ Main”检查设置表“ 010_A”至“ 080_H”,如果匹配,则使用“ Code”的前4位数字检查设置表中的“ Code2”。如果“ Main”和“ Code2”全部匹配,则返回“ Seq”并粘贴到“ Site”列。

以下是我要坚持的地方:

package main

import (
"encoding/json"
"fmt"
)

func main() {
    var jsonBlob = []byte(`[
        {"010_A": [
        {
            "Code1": "010_02",
            "Code2": "010_0231",
            "Seq": "000 Start"
        },
        {
            "Code1": "010_08",
            "OP_CODE": "010_0822",
            "Seq": "010 Shift"
        }
        ],
         "020_B": [{
            "Code1": "020_69",
            "Code2": "020_7011",
            "Seq": "000 ReDo Test"
       },
       {
            "Code1": "020_27",
            "Code2": "",
            "Seq": "000 Redo Combine"
       }
       ],
        "080_H": [
       {
            "Code1": "080_06",
            "Code2": "",
            "Seq": "005 Merge"
       },
       {
            "Code1": "080_43",
            "Code2": "",
            "Seq": "010 Casting"
       },
       {
            "Code1": "080_66",
            "Code2": "080_6621",
            "Seq": "100 Cooling"
       }
       ]}
       ]`)

type Record map[string][]map[string]string
var records []Record

err := json.Unmarshal(jsonBlob, &records)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Printf("%+v\n", records)

if v, s := records[0]["020_B"][0]["Code2"]; s {
    fmt.Println("ok:", v)
    }
}

它只返回第一个结果,更不用说我要执行的下一个功能了(循环输入搜索条件,粘贴到原始数据列中。)

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您想要什么,所以我写了一个代码段:

LOCATION IN ('PUNE','MUMBAI')