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

如何使用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 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的