How to run a "Go" file?

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

!

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.

1 Like

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 ?

What about “Goland” ?

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

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

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 ?

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

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.

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.

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.

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

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.

Hi

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

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

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

Is this right ? i see an error

I tried to debug “hello.go”

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.

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

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