songgao / go.pipeline

go.pipeline is a utility library that imitates unix pipeline. It simplifies chaining unix commands (and other stuff) in Go.
14 stars 1 forks source link

Question sending "name" #1

Closed jtc25 closed 11 years ago

jtc25 commented 11 years ago

I'm trying to run "Gnuplot" with go.pipeline. To execute a surface plot I need to send the name of a file to splot. For example Splot "Filename". How I do I send quoted filename? Any suggestions would be appreciated.

Jim

songgao commented 11 years ago

Hi, are you trying to put Splot "Filename" into the stdin of gnuplot process? Could you be more specific about the issue? If it's about putting quoted filename in a string in Go,

`Splot "Filename"`

should work.

Put any string in back quote (

`

), and they'll be auto escaped. More: http://golang.org/ref/spec#String_literals

Btw, if you are constructing gnuplot scripts in Go, have you looked at this package? Though it might be useful for you.

jtc25 commented 11 years ago

Thank you for your quick response. In the past I have used similar code to control Gnuplot in 'C'. But I'm struggling with GO.The following is my code and still i'm having problems. I must be miss be doing something wrong. It still doesn't work. Any help will very much appreciated.

import ( "fmt" "github.com/songgao/go.pipeline" "text/template" )

func main() { counter := 0

pipeline.NewPipeline().
    C("gnuplot").C("/n/t'Splot "Zsas"').  (line #14 )
    L(func(in string) string {
    counter++
    return fmt.Sprintf("%d:\t%s", counter, in)
}, nil).P()                 (line #18)

}

I get these errors. /main.go:14: syntax error: unexpected name, expecting )
. /main.go:14: newline in string
 ./main.go:18: syntax error: unexpected comma, expecting semicolon or newline 
exit code 2, process exited normally.

On Apr 12, 2013, at 12:22 AM, Song Gao notifications@github.com wrote:

Hi, are you trying to put Splot "Filename" into the stdin of gnuplot process? Could you be more specific about the issue? If it's about putting quoted filename in a string in Go,

Splot "Filename" should work.

Put any string in back quote (

` ), and they'll be auto escaped. More: http://golang.org/ref/spec#String_literals

Btw, if you are constructing gnuplot scripts in Go, have you looked at this package? Though it might be useful for you.

— Reply to this email directly or view it on GitHub.

songgao commented 11 years ago

As I said, you need to use back quote or escape characters manually. You probably need something like this (assuming the gnuplot syntax is right:

package main

import (
    "github.com/songgao/go.pipeline"
)

func main() {
    stdin, stderr := make(chan string), make(chan string)
    close(stderr)
    go func() {
        stdin <- `splot "Zsas"
`
        close(stdin)
    }()
    pipeline.StartPipelineWithStreams(stdin, stderr).C("gnuplot").P()
}
jtc25 commented 11 years ago

Thank you very much. You can consider this issue closed. I did not realize the last back tick had to be on the next line. For your info- my working surface plot program is the following:

var err error
g_gnuplot_cmd, err := exec.LookPath("gnuplot")
if err != nil {
    fmt.Printf("** could not find path to 'gnuplot':\n%v\n", err)
    panic("could not find 'gnuplot'")
}
fmt.Printf("-- found gnuplot command: %s\n", g_gnuplot_cmd)

stdin, stderr := make(chan string), make(chan string)
close(stderr)
go func() {
    stdin <- `splot "Zsas" with pm3d

` close(stdin) }() pipeline.StartPipelineWithStreams(stdin, stderr).C("gnuplot", "-persist").P() }

On Apr 13, 2013, at 9:01 PM, Song Gao notifications@github.com wrote:

As I said, you need to use back quote or escape characters manually. You probably need something like this (assuming the gnuplot syntax is right:

package main

import ( "github.com/songgao/go.pipeline" )

func main() { stdin, stderr := make(chan string), make(chan string) close(stderr) go func() { stdin <- splot "Zsas" close(stdin) }() pipeline.StartPipelineWithStreams(stdin, stderr).C("gnuplot").P() } — Reply to this email directly or view it on GitHub.

songgao commented 11 years ago

The reason why it's in the second line is that, you need a line break for gnuplot to execute the command. You can also use escaping form:

stdin <- "splot \"Zsas\" with pm3d\n"

Anyway, good luck with whatever you are building. If the input of gnuplot is not quite complicated and does not required Go to process, your probably wanna consider shell scripting, in which the code will be more concise.