Golang using Graphviz

Source: Internet
Author: User
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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.