Installing Golang on CentOS
To start Web development with Golang on CentOS, first install the Go environment. You can use either yum (simpler) or manual installation (more control). For manual installation:
go1.21.0.linux-amd64.tar.gz) from the official website./usr/local:sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gz
/etc/profile.d/golang.sh:export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
source /etc/profile.d/golang.sh
go version, which should display the installed Go version.Creating a Simple Web Application
With Go installed, create a basic Web server using the standard net/http package (for simplicity) or a framework like Gin. Here’s an example using net/http:
mkdir mywebserver && cd mywebserver
go mod init example.com/mywebserver
main.go:package main
import (
"fmt"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
func main() {
http.HandleFunc("/", helloHandler)
fmt.Println("Server running on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println("Error starting server:", err)
}
}
go run main.go
http://your_server_ip:8080 to see “Hello, World!”.Using a Web Framework (Gin Example)
For more structured and scalable Web development, use a framework like Gin (a high-performance HTTP router). Here’s how to set it up:
go get -u github.com/gin-gonic/gin
main.go file with Gin:package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "Hello, World!"})
})
r.Run() // Defaults to :8080
}
go run main.go
http://your_server_ip:8080 to see a JSON response: {"message":"Hello, World!"}.Deploying to Production
For production, you need to manage processes, configure firewalls, and use a reverse proxy. Here’s a step-by-step guide:
firewalld:sudo systemctl start firewalld
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
/etc/systemd/system/mywebserver.service) to manage your Go app:[Unit]
Description=My Go Web Server
After=network.target
[Service]
User=your_username
Group=your_groupname
ExecStart=/usr/bin/go run /path/to/your/webserver/main.go
Restart=always
Environment=GOPATH=/home/your_username/go
[Install]
WantedBy=multi-user.target
Reload systemd, start the service, and enable it to run on boot:sudo systemctl daemon-reload
sudo systemctl start mywebserver
sudo systemctl enable mywebserver
/etc/nginx/conf.d/mywebserver.conf):server {
listen 80;
server_name your_domain_or_ip;
location / {
proxy_pass http://localhost:8080; # Forward to your Go app
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Test the Nginx configuration, reload it, and restart the service:sudo nginx -t
sudo systemctl reload nginx
sudo systemctl restart nginx
Now, your Go app is accessible via http://your_domain_or_ip, with Nginx handling SSL termination, load balancing, and static files.