How to Install Go on Linux and Set Up VSCode Remote Debugging
This guide walks you through downloading the Go source, manually installing it on a Linux server, configuring workspace and environment variables, and then using VSCode for remote development, debugging, and handling command‑line arguments and environment variables in Go programs.
Downloading Go Source
You can install Go via package managers like yum or apt-get, but this tutorial uses the source package for broader compatibility. Download the latest tarball from the official site (e.g., https://golang.org/dl/).
Manual Installation
Extract and Install
Upload the tarball (e.g., go1.14.2.linux-amd64.tar.gz) to your remote Linux server and run:
tar -zxvf -C /usr/local/ go1.14.2.linux-amd64.tar.gzCreate a Workspace
Set up a directory structure for your Go projects:
mkdir GoPath
mkdir -p GoPath/src
mkdir -p GoPath/bin
mkdir -p GoPath/pkgThe directories serve the following purposes:
src : source files (.go, .c, .h, .s, etc.)
pkg : compiled package files (.a)
bin : compiled executable binaries
Configure Environment Variables
Add the following lines to /etc/profile and reload the file:
export GOROOT=/usr/local/go
export GOPATH=/yourpath/GoPath # replace with your actual path
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin # source /etc/profile # apply immediatelyVerify Installation
# go version
# go version go1.14.2 linux/amd64If the version information is displayed, the installation succeeded.
Remote Development with VSCode
To avoid logging into the server for every edit, configure VSCode for remote Go development. Install the official Go extension and any suggested auxiliary extensions.
After installing, click “install all” to add the remaining recommended extensions.
Hello World Example
Every language tutorial starts with a Hello World program. Create HelloWorld.go with the following content:
package main
import "fmt"
func main() {
fmt.Print("hello world", " i am ready to go :)
")
fmt.Println("hello world", "i am ready to go :)")
}fmt Package Overview
The fmt package provides C‑style formatted I/O. Two common functions are: Print: func Print(a ...interface{}) (n int, err error) – prints arguments with spaces when needed. Println: func Println(a ...interface{}) (n int, err error) – always adds a trailing newline.
Debugging
Terminal Debugging
Run the program directly:
# go run HelloWorld.go
hello world i am ready to go :)
hello world i am ready to go :)Or compile first:
# go build HelloWorld.go
# ./HelloWorld
hello world i am ready to go :)
hello world i am ready to go :)VSCode Debugging
Press F5 in VSCode to start a debugging session. The console will display the same output as the terminal.
Command‑Line Arguments
Retrieve arguments using os.Args:
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
fmt.Println(os.Args)
for idx, arg := range os.Args {
fmt.Println("参数"+strconv.Itoa(idx)+":", arg)
}
}Run with arguments:
# go run basic.go argv1 argv2
[/tmp/go-build441686724/b001/exe/basic argv1 argv2]
参数0: /tmp/go-build441686724/b001/exe/basic
参数1: argv1
参数2: argv2In VSCode, set the args field in launch.json to pass parameters during debugging.
Environment Variable Access
Read environment variables with os.Getenv:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(os.Getenv("type"), os.Getenv("name"), os.Getenv("GOROOT"))
}In VSCode, you can also define environment variables in launch.json using the same args syntax (key:value pairs). In a Linux terminal, set them with export, e.g., export GOROOT=/usr/local/go, then retrieve with os.Getenv("GOROOT").
Conclusion
You now have a fully functional Go development environment on Linux that supports remote editing and debugging via VSCode, along with examples for handling command‑line arguments and environment variables. Continue with the next article to explore Go’s basic syntax in depth.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
