Edward's Tech Site

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

FORAY: Sep 09, 2023 - Golang
Install Go on Hetzner Arch machine and create script to create HTML that is accessible on subdomain

CONTINUE: start working through book, data types

  • set up go.tkserv.eu subdomain
  • now install Go
  • create Go script that creates a text file
    • asked ChatGPT
    • creating repository so I can copy it in, difficult in terminal to copy
      • created repository
      • initialized local repository, git init -b dev
      • pushing existing repository
      • made file, git add ., git commit -m "initial commit"
      • git remote add origin git@github.com:edwardtanguay/et034-golang-site.git
      • git push -u origin dev
      • it's in the repository
    • gensite.go
      • package main
         
        import (
        "fmt"
        "os"
        )
         
        func main() {
        // Define the file name
        fileName := "example.txt"
         
        // Create or open the file for writing, create it if it doesn't exist, truncate if it does
        file, err := os.Create(fileName)
        if err != nil {
        fmt.Println("Error creating file:", err)
        return
        }
        defer file.Close()
         
        // Write content to the file
        content := "Hello, this is some text written to the file!"
        _, err = file.WriteString(content)
        if err != nil {
        fmt.Println("Error writing to file:", err)
        return
        }
         
        fmt.Println("File created and content written successfully.")
        }
    • git pull worked
    • when run, it locks up
    • but then finishes after 20 seconds
    • then worked:
    • ok, then worked:
  • now create an HTML file
    • backticks work just like in JavaScript
    • the script has been created in current directory
  • now create the file that is served on the site
    • gensite.go - works well
      • package main
         
        import (
        "fmt"
        "os"
        )
         
        func main() {
        fileName := "/var/www/go/index.html"
        file, err := os.Create(fileName)
        if err != nil {
        fmt.Println("Error creating file:", err)
        return
        }
        defer file.Close()
         
        content := `
        <html>
        <head>
        <title>Go Site</title>
        <style>
        body {
        background-color: #333;
        color: #ccc;
        font-family: sans-serif;
        margin: 1rem;
        }
        a {
        color: #dce378;
        }
        </style>
        </head>
        <body>
        <h1>Go Site</h1>
        <p>This file was generated by a Go script.</p>
        <p>Currently learning Go with this free, online book: <a target="_blank" href="https://www.programming-books.io/essential/go/basic-types-2803d5d5229f4932af82a1dcc86eb8bf">Essential Go</a>
        </body>
        </html>
        `
        _, err = file.WriteString(content)
        if err != nil {
        fmt.Println("Error writing to file:", err)
        return
        }
         
        fmt.Println("File created and content written successfully.")
        }
  • resources
  • NEXT STEPS:
    • )) start working through book, data types