Edward's Tech Site

this site made with Next.js 13, see the code

HOWTO: May 14 - Go
Install Go on a new Hetzner Debian machine and make universal executable Go program
  • installing Go on Debian using these instructions
    • Go is installed on Arch but not on Debian
    • get latest version from: ../dlhttps://go.dev/dl
      • currently 1.22.3 (2024-05-14)
    • download Go package including version you want
      • wget https://golang.org/dl/go1.22.3.linux-amd64.tar.gz
    • unpack it
      • sudo tar -C /usr/local -xzf go1.22.3.linux-amd64.tar.gz
    • set the environment
      • ~/.profile - add to end of file
        • export PATH=$PATH:/usr/local/go/bin
          export GOPATH=$HOME/go
          export PATH=$PATH:$GOPATH/bin
      • should look something like this:
    • load changes for current session
      • source ~/.profile
    • verify installation
      • go version
  • make useful Go program that you can execute in any directory
    • this program lists out all files in a directory with the number of lines they contain
    • create file
      • create projects/listfiles
      • listfiles.go
        • package main
           
          import (
          "fmt"
          "io/ioutil"
          "log"
          "strings"
          )
           
          func countLines(filename string) (int, error) {
          content, err := ioutil.ReadFile(filename)
          if err != nil {
          return 0, err
          }
          lines := strings.Split(string(content), "\n")
          return len(lines), nil
          }
           
          func main() {
          files, err := ioutil.ReadDir(".")
          if err != nil {
          log.Fatal(err)
          }
           
          for _, file := range files {
          if file.IsDir() {
          continue
          }
          filename := file.Name()
          lines, err := countLines(filename)
          if err != nil {
          log.Printf("Error reading %s: %v\n", filename, err)
          continue
          }
          fmt.Printf("%s: %d lines\n", filename, lines)
          }
          }
    • compile it
      • go build listfiles.go
    • copy the created file to a place where it can be execute anywhere
      • sudo mv listfiles /usr/local/bin/
    • execute it anywhere