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))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示