This is a creation in Article, where the information may have evolved or changed.
Graphviz's Introduction Please refer to: http://www.cnblogs.com/ghj1976/p/4539788.html
Installing Graphviz
The version of the corresponding operating system needs to be downloaded in http://www.graphviz.org/Download_macos.php.
After the installation is complete, you can use the DOT–V command to view the installed version and confirm the installation
After the MAC is installed, there will be a Graphviz app that can view the *.GV file as shown in the effect:
Generate Graphviz File
The encapsulation method for generating Graphviz files with Golang is as follows:
Https://github.com/awalterschulze/gographviz
The sample code we use it is as follows:
Package Main
Import (
"FMT"
"Github.com/awalterschulze/gographviz"
)
Func Main () {
Graphast, _: = Gographviz. Parse ([]byte (' digraph g{} '))
Graph: = Gographviz. Newgraph ()
Gographviz. Analyse (graphast, graph)
Graph. AddNode ("G", "a", nil)
Graph. AddNode ("G", "B", nil)
Graph. Addedge ("A", "B", true, nil)
Fmt. Println (graph. String ())
}
The execution effect is as follows:
The command line that uses DOT to generate PNG is as follows:
Dot 11.gv-t png-o 11.png
The complete Go code is as follows:
Package Main
Import (
"Bytes"
"FMT"
"Github.com/awalterschulze/gographviz"
"Io/ioutil"
"Os/exec"
)
Func Main () {
Graphast, _: = Gographviz. Parse ([]byte (' digraph g{} '))
Graph: = Gographviz. Newgraph ()
Gographviz. Analyse (graphast, graph)
Graph. AddNode ("G", "a", nil)
Graph. AddNode ("G", "B", nil)
Graph. Addedge ("A", "B", true, nil)
Fmt. Println (graph. String ())
Output file
Ioutil. WriteFile ("11.GV", []byte (graph). String ()), 0666)
Create picture
System ("Dot 11.gv-t png-o 12.png")
}
The method that invokes the system instruction, the parameter s is the shell command called
Func system (s string) {
CMD: = Exec.command ('/bin/sh ', '-C ', s)//Call Command function
var out bytes. Buffer//Buffered bytes
Cmd. Stdout = &out//Standard output
ERR: = cmd. Run ()//running instructions, making judgments
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. Printf ("%s", out. String ())//Output execution result
}
Generated by: