doulifang5554 2018-05-02 23:40
浏览 110
已采纳

如何使用Go程序中的点脚本运行graphviz进程?

I want to run a dot script that outputs to an image.
How do I call it from Golang?

package main

import (
"fmt"

"os/exec"
)

func main() {
path, err := exec.LookPath("dot")
cmd := exec.Command(path, "-Tpng", "/Users/arafat/Desktop/dev/go/src/github.com/Arafatk/dataviz/DotExamples/arraylist.dot", ">", "/Users/arafat/Desktop/dev/go/src/github.com/Arafatk/dataviz/hello.png")
err = cmd.Run()

fmt.Println(path)
if err != nil {
    println(err.Error())
    return
}
}

This is my code which gives exit code 3.

@zerkms
Sorry, I can do that I am just confused because this code does not give any output except nil.

path, _ := exec.LookPath("dot")       
cmd := exec.Command(path, "-Tpng", "/Users/arafat/Desktop/dev/go/src/github.com/Arafatk/dataviz/DotExamples/arraylist.dot")        
out := cmd.Run()       
fmt.Println(out)         

But this command line function works

dot -Tpng  /Users/arafat/Desktop/dev/go/src/github.com/Arafatk/dataviz/DotExamples/arraylist.dot           

Can you tell me how to actually use the function above in Golang?

  • 写回答

1条回答 默认 最新

  • douxiexie3574 2018-05-03 09:15
    关注

    As mentioned before by @zerkms you can use cmd.StdoutPipe() instead of ">".
    However there is a much simpler way to solve this problem

    package main
    
    import (
       "io/ioutil"
       "os"
       "os/exec"
    )
    
    func main() {
    path, _ := exec.LookPath("dot")
    cmd, _ := exec.Command(path, "-Tpng", "/Users/arafat/Desktop/dev/go/src/github.com/Arafatk/dataviz/DotExamples/arraylist.dot").Output()
    mode := int(0777)
       ioutil.WriteFile("outfile.png", cmd, os.FileMode(mode))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 docker环境配置
  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现