Go, also known as Golang, is a fast, simple, and powerful programming language developed by Google. It is widely used for building command-line tools, web applications, APIs, automation scripts, cloud applications, and more.
If you use an Android device, you can easily install and run Go programming language using Termux. In this guide, you will learn how to install Go in Termux, verify the installation, create your first Go program, and solve common errors.
Table of Contents
- Requirements
- Step 1: Update Termux
- Step 2: Install Go
- Step 3: Verify Installation
- Step 4: Create Your First Go Program
- Step 5: Run the Go Program
- Step 6: Build a Go Program
- Using Go Modules
- Useful Go Commands
- Common Errors and Fixes
- FAQ
Requirements
Before installing Go, make sure you have:
- An Android smartphone or tablet
- Termux installed and working
- A stable internet connection
- Enough free storage space
Tip: It is recommended to use a recent version of Termux from a trusted source rather than an outdated app-store build.
Step 1: Update Termux Packages
First, open Termux and update all installed packages.
pkg update && pkg upgrade -y
This command updates the Termux package lists and upgrades installed packages.
You can also install some useful tools:
pkg install git nano wget -y
Step 2: Install Go (Golang) in Termux
Install Go using the following command:
pkg install golang -y
Termux will download and install the Go compiler and required dependencies.
Wait until the installation is complete.
Step 3: Verify Go Installation
After installation, check the installed Go version:
go version
You should see output similar to:
go version go1.xx.x android/arm64
The exact version may be different depending on the current Termux repository.
You can also check the Go environment:
go env
Step 4: Create Your First Go Program
Create a new directory for your Go project:
mkdir hello-go
cd hello-go
Now create a file called main.go:
nano main.go
Add the following Go code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Save the file in Nano:
- Press CTRL + O
- Press Enter
- Press CTRL + X
Step 5: Run the Go Program
Run your Go program using:
go run main.go
Output:
Hello, World!
Congratulations! 🎉 You have successfully installed and run Go programming language in Termux.
Step 6: Build a Go Program
You can compile your Go program into an executable file.
go build main.go
Check the files:
ls
Run the compiled program:
./main
Using Go Modules in Termux
For modern Go projects, it is recommended to use Go Modules.
Inside your project directory, run:
go mod init hello-go
This creates a go.mod file.
For example:
mkdir myproject
cd myproject
go mod init myproject
You can now create your Go files and manage project dependencies.
To download and organize dependencies, use:
go mod tidy
Useful Go Commands in Termux
| Command | Description |
|---|---|
go version |
Check installed Go version |
go env |
Display Go environment settings |
go run main.go |
Run a Go program |
go build |
Compile the project |
go mod init project-name |
Create a Go module |
go mod tidy |
Clean and download dependencies |
go test |
Run project tests |
go fmt |
Format Go source code |
Install Git for Go Projects
Many Go projects are hosted on Git repositories. Install Git with:
pkg install git -y
Check the version:
git --version
Clone a repository:
git clone REPOSITORY_URL
Then enter the project directory:
cd project-name
If the project uses Go Modules:
go mod tidy
Run the application:
go run .
Common Errors and Fixes
1. Command Not Found: go
If you see:
go: command not found
Install Go again:
pkg install golang
Then restart Termux and check:
go version
2. Package Repository Error
Try updating the repositories:
pkg update
termux-change-repo
Select an available repository mirror and then run:
pkg update && pkg upgrade -y
pkg install golang -y
3. Permission Denied Error
If you build an executable but cannot run it, make sure you are running it from the correct directory:
ls
./main
Avoid trying to execute files directly from Android shared storage, as execution restrictions may apply.
Create a Simple Go Calculator
Create a new file:
nano calculator.go
Paste the following code:
package main
import "fmt"
func main() {
var a, b int
fmt.Print("Enter first number: ")
fmt.Scanln(&a)
fmt.Print("Enter second number: ")
fmt.Scanln(&b)
fmt.Println("Sum:", a+b)
}
Run it:
go run calculator.go
Recommended Project Structure
my-go-project/
│
├── go.mod
├── main.go
│
├── internal/
│
├── pkg/
│
└── README.md
Start with a simple structure and organize your project as it grows.
Why Use Go in Termux?
- Write and test Go programs from your Android device.
- Learn programming without needing a computer.
- Build command-line utilities.
- Practice Git and Go Modules.
- Develop automation and productivity tools.
- Experiment with APIs and backend programming.
- Compile and test small projects on the go.
Frequently Asked Questions
Can I install Go on Android?
Yes. You can install Go on Android using Termux and write, compile, and run Go programs directly from your device.
What command installs Go in Termux?
pkg install golang -y
How do I check my Go version?
go version
Can I use Go Modules in Termux?
Yes. Create a module using:
go mod init project-name
Can I learn Go using only an Android phone?
Yes. Termux provides a Linux-like command-line environment where you can write and run Go programs. For larger projects, however, a computer may provide a more comfortable development experience.
Conclusion
Installing Go (Golang) in Termux is simple. Update your packages, install the Golang package, verify the installation, and start creating Go programs.
pkg update && pkg upgrade -y
pkg install golang -y
go version
Once Go is installed, you can use your Android device to learn programming, build command-line tools, practice Go Modules, and develop small projects from anywhere.
Quick Installation Commands
pkg update && pkg upgrade -y
pkg install golang git nano -y
go version







0 comments:
Post a Comment