How to run a "Go" file?


(Paul A. Gureghian) #1

I open the file in terminal and it just opens VSC. I don’t see a run button in VSC.

!


(Ashish Shekar) #2

Hi, VSCode tends to be a lot like text editor as much as it tries to be an IDE.

There can be 2 ways to run your file.

  1. Use Ctrl + ~ to open a terminal window and write go run sandbox.go.

  2. Using launch configs. Use Ctrl/Cmd + Shift + D to open debug window. Now click on the settings icon, you’ll now see a JSON file.

Replace inside object with this one.

        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/sandbox.go",
            "env": {},
            "args": []
        }

program key specifies the path to your file. Now you can use that Green run button to run your go program.

Hope this helps.


I have Go installed and in my system PATH
(Paul A. Gureghian) #3

What about all the PATH stuff ? there is the path to the binary and the path for GOPATH. my call to Go works in the terminal, but what about GOPATH ?


(Paul A. Gureghian) #4

What about “Goland” ?


(Yamil Bracho) #5

Check https://github.com/golang/go/wiki/GOPATH


(Paul A. Gureghian) #6

I got “go run” and “go build” to work, couldn’t get the debugger to work the way you showed.


(Paul A. Gureghian) #7

Do I add the path where the file lives or the go installation ? and, I need to change the file name each time I run a different file ?


(Eric Lindblad) #8

Y. Bracho provided a link to a Wiki Page so I won’t reproduce that link here.

FileTreeDocumentation

On Windows using cmd.exe you will find the console variables by running the builtin ‘SET’, and also in cmd.exe running ‘go.exe env’ will clarify what the GOPATH value is.

Test your installation

I presume from your screenshots that you are running Windows 7.

https://nurmi-labs.blogspot.com/2016/11/git.html


(George Calianu) #9

If you want more Go related features try LiteIDE which is specialised IDE for Go language not a general purpose editor.


(Ashish Shekar) #10

Hi Paul, As I have shown above that you need to change only sandbox.go part to your file name.

The ${workspaceFolder} tells VSCode that, go to the current folder from which VSCode was opened and then run this file called hello.go

so your config should look like this and not ${/Go}

        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/hello.go",
            "env": {},
            "args": []
        }

As far as goland is considered, you can directly run the main file by clicking on the run button above since go allows single entry point to (ie: func/package main) it’ll run where the main function lies.


(Paul A. Gureghian) #11

Do I need to change the file name for each file I want to run ? and should it be .go or .exe ?

As for Goland, it is premium ware, so maybe I will try LiteIDE.


(Norbert Melzer) #12

Just use your editor to edit the source code, nothing else.

Then use go build/go run on the terminal to let them do their thing.


(Paul A. Gureghian) #13

You may be right. I have figured out how to do it exactly that way with “run” and “build” in the terminal.


(Paul A. Gureghian) #14

I save the file then hit the green debug arrow and it has me choose an env and I choose Go and nothing seems to happen.


(Johan Dahl) #15

Hi

program should be path to go.exe and arguments should be

[“run”, ${workspaceFolder}/hello.go]


(Paul A. Gureghian) #16

Can you show the syntax which also has the path to the go.exe file ?


(Paul A. Gureghian) #17

Is this right ? i see an error


(Paul A. Gureghian) #18

I tried to debug “hello.go”


(Ashish Shekar) #19

Why did you eat the configurations key in launch.json? Without that, you won’t see anything on Launch dropdown. (it’ll always show No configuration)

Since you are using windows I don’t know exactly how to point to go but giving an absolute path to your go.exe should work.


(Johan Dahl) #20

No. It is the args key which should hold the arguments.

And could you post your configuration here as text. Between two lines which is only three backticks . Like this :

```
{
key: “xxx”, …
}
```
And it will be nicely formatted. Screen shots are hard to read on a mobile