From 9adbaf69eed2c38792853ca4902ce0a718253f93 Mon Sep 17 00:00:00 2001 From: webmin Date: Thu, 5 Mar 2020 14:16:53 +0800 Subject: [PATCH] add golang demo --- demo/golang/.vscode/launch.json | 21 ++++++++ demo/golang/demo.go | 91 +++++++++++++++++++++++++++++++++ demo/golang/go.mod | 3 ++ 3 files changed, 115 insertions(+) create mode 100644 demo/golang/.vscode/launch.json create mode 100644 demo/golang/demo.go create mode 100644 demo/golang/go.mod diff --git a/demo/golang/.vscode/launch.json b/demo/golang/.vscode/launch.json new file mode 100644 index 0000000..a43be50 --- /dev/null +++ b/demo/golang/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Launch", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}", + "env": {}, + "args": [], + "dlvLoadConfig": { + "followPointers": true, + "maxVariableRecurse": 1, + "maxStringLen": 5000, + "maxArrayValues": 64, + "maxStructFields": -1 + } + } + ] +} \ No newline at end of file diff --git a/demo/golang/demo.go b/demo/golang/demo.go new file mode 100644 index 0000000..2cfecc7 --- /dev/null +++ b/demo/golang/demo.go @@ -0,0 +1,91 @@ +package main + +import ( + "encoding/json" + "math/rand" + "strconv" + "time" +) + +// Debug using Delve needs to adjust the configuration +// Delve's maxStringLen default is small and needs to be adjusted for the maxStringLen default of the delve. See in detail (microsoft/vscode-go#868). +// Add the following configuration to +//launch.json +// "dlvLoadConfig": { +// "followPointers": true, +// "maxVariableRecurse": 1, +// "maxStringLen": 5000, +// "maxArrayValues": 64, +// "maxStructFields": -1 +// } + +// debuging test Must be set settings.json +//settings.json +// "go.delveConfig": { +// "dlvLoadConfig": { +// "followPointers": true, +// "maxVariableRecurse": 1, +// "maxStringLen": 5000, +// "maxArrayValues": 64, +// "maxStructFields": -1 +// }, +// "apiVersion": 2, +// "showGlobalVariables": true +// } + +type Graph struct { + Kind map[string]bool `json:"kind"` + Nodes []NodeGraphData `json:"nodes"` + Edges []EdgeGraphData `json:"edges"` +} + +type NodeGraphData struct { + ID string `json:"id"` + Label string `json:"label,omitempty"` + Color string `json:"color,omitempty"` + Shape string `json:"shape,omitempty"` +} + +type EdgeGraphData struct { + From string `json:"from"` + To string `json:"to"` + Label string `json:"label,omitempty"` + ID string `json:"id"` + Color string `json:"color,omitempty"` + Dashes bool `json:"dashes,omitempty"` +} + +func NewGraph() *Graph { + return &Graph{ + Kind: map[string]bool{"graph": true}, + Nodes: []NodeGraphData{}, + Edges: []EdgeGraphData{}, + } +} + +func (this *Graph) toString() string { + rs, _ := json.Marshal(this) + return string(rs) +} + +// Visualize "s" +func main() { + rand.Seed(time.Now().UnixNano()) + graph := NewGraph() + var s string + for i := 2; i < 100; i++ { + id := strconv.Itoa(i) + graph.Nodes = append(graph.Nodes, NodeGraphData{ + ID: id, + Label: id, + }) + targetId := rand.Intn(i) + 1 + graph.Edges = append(graph.Edges, EdgeGraphData{ + From: id, + To: strconv.Itoa(targetId), + }) + s = graph.toString() + _ = s + //fmt.Printf("%s", s) + } +} diff --git a/demo/golang/go.mod b/demo/golang/go.mod new file mode 100644 index 0000000..7d7d334 --- /dev/null +++ b/demo/golang/go.mod @@ -0,0 +1,3 @@ +module demo + +go 1.13