Why Go is the Ideal Language for Automation?
\n\nHave you ever written a Python script that worked perfectly on your laptop but crashed on the server due to a missing interpreter version? Or perhaps your Bash script turned into an unmaintainable monster when the logic became a bit more complex than "copy a file"?
\n\nIf so, you should take a closer look at Go. This language was created within Google specifically to solve the problems engineers face when writing utilities and services. It compiles into a single binary (no dependencies!), runs at lightning speed, and has a simple yet powerful syntax.
\n\nIn this article, we won't teach you the basics of syntax. We'll get straight to the point: task automation with Go using real code examples. You'll learn how to write your first "Go scripts" that will run more reliably and faster than their Python counterparts. Let's go!
\n\n1. Working with the File System: Cleaning Logs Like a Pro
\n\nOne of the most common tasks for an admin or developer is cleaning up old logs. Let's write a utility that deletes files older than N days in a given directory. This is a classic example of automation with Go.
\n\npackage main\n\nimport (\n "fmt"\n "os"\n "path/filepath"\n "time"\n)\n\nfunc main() {\n // Path to the log directory\n logDir := "/var/log/myapp"\n // Delete files older than 7 days\n maxAge := 7 * 24 * time.Hour\n\n err := filepath.Walk(logDir, func(path string, info os.FileInfo, err error) error {\n if err != nil {\n return err\n }\n\n // Skip directories\n if info.IsDir() {\n return nil\n }\n\n // Check file age\n if time.Since(info.ModTime()) > maxAge {\n fmt.Printf("Deleting old file: %s (age: %v)\\n", path, time.Since(info.ModTime()))\n err := os.Remove(path)\n if err != nil {\n fmt.Printf("Error deleting %s: %v\\n", path, err)\n }\n }\n return nil\n })\n\n if err != nil {\n fmt.Printf("Error walking directory: %v\\n", err)\n }\n}\n\n\nThis code is a great example of how to automate tasks with Go without extra libraries. The built-in filepath package recursively traverses folders, and os.Remove frees up space. By compiling it once, you get a binary that can be run via cron on any server.
2. HTTP Requests and API Parsing
\n\nSuppose you need to monitor the status of several microservices or parse data from an external API. Go with its goroutines handles this brilliantly. Let's write a script that checks website availability and sends a notification if something goes down. This is another powerful example of automation with Golang.
\n\npackage main\n\nimport (\n "fmt"\n "net/http"\n "sync"\n "time"\n)\n\nfunc checkURL(url string, wg *sync.WaitGroup) {\n defer wg.Done()\n\n client := http.Client{\n Timeout: 5 * time.Second,\n }\n\n resp, err := client.Get(url)\n if err != nil {\n fmt.Printf("[FAIL] %s - %v\\n", url, err)\n return\n }\n defer resp.Body.Close()\n\n if resp.StatusCode != http.StatusOK {\n fmt.Printf("[WARN] %s - status: %d\\n", url, resp.StatusCode)\n } else {\n fmt.Printf("[OK] %s - is working\\n", url)\n }\n}\n\nfunc main() {\n urls := []string{\n "https://google.com",\n "https://github.com",\n "https://pythonlib.ru",\n "https://non-existent-site.xyz",\n }\n\n var wg sync.WaitGroup\n\n for _, url := range urls {\n wg.Add(1)\n go checkURL(url, &wg) // Launch in a goroutine\n }\n\n wg.Wait() // Wait for all goroutines to finish\n fmt.Println("Check completed.")\n}\n\n\nNotice the s