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

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答