Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang example #54

Closed
prvst opened this issue Jul 27, 2017 · 19 comments · Fixed by #55
Closed

Golang example #54

prvst opened this issue Jul 27, 2017 · 19 comments · Fixed by #55

Comments

@prvst
Copy link

prvst commented Jul 27, 2017

Is there an example on how to use the GO language engine? If not, would it be possible to add one?
The language engine itself is already available, I'm just not too sure how to load and run.

Thanks

@yihui
Copy link
Owner

yihui commented Jul 27, 2017

The go engine was added from yihui/knitr#1330 by @hodgesds. Perhaps we should add a minimal example to this repo.

@prvst
Copy link
Author

prvst commented Jul 27, 2017

Just a side note here; I tried configuring the block like this:

{r golang, engine='go', engine.opts='run', engine.path='/usr/local/go/bin/go'}

but I get the following:

go run: no go files listed

It's the same output when I run go run on my terminal without passing a source to execute. I'm not sure but it looks to me that there might be something wrong or missing with the engine configuration

@hodgesds
Copy link
Contributor

hodgesds commented Jul 28, 2017

Here is an example of using a go package (non executable).

Hello Go!
```{go eval=FALSE}
package hello 

func HelloWorld(){
   println("hello world!")
}```

And for a main (executable) package:

I can run Go!
```{go}
package main 

func main(){
   println("hello world!")
}```

Note that it will automatically call go fmt on the source.

@hodgesds
Copy link
Contributor

One last thought... is go on your $PATH? I'm not sure if the code handles engine.path.

@yihui yihui closed this as completed in #55 Jul 28, 2017
@prvst
Copy link
Author

prvst commented Jul 28, 2017

Sorry but that still doesn't work.
I created a fresh notebook from Rstudio and copied the snippets above. The blocks are not recognized as 'runable' ( no play button on the right corner). I tried to execute all anyways but nothing happens.

Next I added the following to my file:

library(knitr)
knit_engines$get("go")

The blocks still are not recognized, but now when I execute the file, the block that contains the main function returns the following:

/bin/sh: 1: go: not found

I double checked and my Go installation is on path, I also can confirm that because its my default language for development and I use other tools like Atom.

@prvst
Copy link
Author

prvst commented Jul 28, 2017

Also, I had to change the snippet from

{go}

To

{r golang, engine='go'}

to get the error message

@hodgesds
Copy link
Contributor

What happens when you run system("echo $PATH")? The error /bin/sh: 1: go: not found would indicate that go isn't on your $PATH

@prvst
Copy link
Author

prvst commented Jul 28, 2017

When I run the command from Rstudio I see this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

But when I check the $PATH from my terminal I get this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/bin:/home/felipevl/go/bin

Note how they are different.
Am I missing something ?

@hodgesds
Copy link
Contributor

hodgesds commented Jul 28, 2017

This might be of help, perhaps try starting Rstudio from the terminal if you want to inherit the same path.

touch ~/.Renviron | R_PATH="PATH=$PATH" | echo $R_PATH >  ~/.Renviron

@prvst
Copy link
Author

prvst commented Jul 28, 2017

I added my system PATH to the file as described on that link you sent me, and now I started rstudio from terminal. When I run system("echo $PATH") I see the following:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/bin:/home/felipevl/go/bin

Now it seems to be present on the path inside Rstudio as well.

I typed the same block again

{r golang, engine='go'}
package main 

func main(){
   println("hello world!")
}

And now I'm getting the following :

go: unknown subcommand "/tmp/RtmpxYjuJJ/chunk-code31bd34e27ecb." Run 'go help' for usage.

The block is still unrecognized as 'runnable'

@hodgesds
Copy link
Contributor

Can you try running the minimal example in this repo:
./k 118-engine-go.Rmd

@prvst
Copy link
Author

prvst commented Jul 29, 2017

I opened Rstudio via command line and checked the path, the Go installation is present.

I downloaded the file and created a new notebook. None of the blocks are recognized as executable and only the last one has the code colored.

I clicked the 'Run All ' command and only the last block produced the following output:

go: unknown subcommand "/tmp/RtmpNJF891/chunk-code220b54e7b85."
Run 'go help' for usage.

@prvst
Copy link
Author

prvst commented Jul 29, 2017

It looks like that the go compiler is being found and is responding, but the block or the execution unit is not being passed to it.

I changed the last block to this {r engine='go', engine.opts='run'} and the output changed to:

go run: no go files listed

go run is what you run when trying a script like that, its just saying that there is nothing to execute.

@hodgesds
Copy link
Contributor

I can't really help you debug your RStudio setup, please run the minimal example that is in this repo.

> require('knitr')
> knit('~/git/knitr-examples/118-engine-go.Rmd')

processing file: ~/git/knitr-examples/118-engine-go.Rmd
  |...........                                                      |  17%
  ordinary text without R code

  |......................                                           |  33%
label: unnamed-chunk-1 (with options) 
List of 2
 $ eval  : logi FALSE
 $ engine: chr "go"

  |................................                                 |  50%
  ordinary text without R code

  |...........................................                      |  67%
label: unnamed-chunk-2 (with options) 
List of 1
 $ engine: chr "go"

running: go run ./code77451bd44b11.go
  |......................................................           |  83%
  ordinary text without R code

  |.................................................................| 100%
label: unnamed-chunk-3 (with options) 
List of 1
 $ engine: chr "go"

running: go run ./code7745662a0cba.go

output file: 118-engine-go.md

[1] "118-engine-go.md"

The contents of 118-engine-go.md should be as follows:

Hello package!

package hello 

func HelloWorld(){
   println("hello world!")
}

Hello main!

package main 

func main(){
   println("hello world!")
}
## hello world!

Or specify an engine:

package main

func main(){
   println("hello world!")
}
## hello world!

@prvst
Copy link
Author

prvst commented Jul 29, 2017

yes I got the same thing.

@prvst
Copy link
Author

prvst commented Jul 29, 2017

When running on the terminal I see the hidden go file being created, but I think the same is not true when running on Rstudio. It looks to me that the code should be on the /tmp folder but the same has nothing there. Could this snippet be the problem ? f = tempfile("code", ".", fileext = ".go")

@prvst
Copy link
Author

prvst commented Jul 29, 2017

The code you have, when running on Rstudio output this:

go: unknown subcommand "/tmp/RtmprHeIZi/chunk-code2f28c15b829."

See how the path ends with a . instead of an full extension ?

@hodgesds
Copy link
Contributor

interesting.... I'm guessing something here needs updated. If you trying using the knit command for a .Rmd file it should work.

@prvst
Copy link
Author

prvst commented Aug 1, 2017

Hey @hodgesds ; Is there any perspective to have this fixed in the near future ?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants