176 lines
3.4 KiB
Markdown
176 lines
3.4 KiB
Markdown
snippets
|
|
|
|
js to go mappings
|
|
[js-to-go.html](../../../_resources/js-to-go.html)
|
|
[js-to-go_1.html](../../../_resources/js-to-go_1.html)
|
|
|
|

|
|
|
|
tracking time
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func myExpensiveFunction() {
|
|
// 1. Tracks starting time and defers the calculation
|
|
defer func(start time.Time) {
|
|
fmt.Printf("Function took: %v\n", time.Since(start))
|
|
}(time.Now())
|
|
|
|
// 2. Your actual function logic here
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
|
|
func main() {
|
|
myExpensiveFunction()
|
|
}
|
|
|
|
```
|
|
|
|
for printing tabular data
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"text/tabwriter"
|
|
)
|
|
|
|
func main() {
|
|
// Initialize tabwriter: output, minwidth, tabwidth, padding, padchar, flags
|
|
w := tabwriter.NewWriter(os.Stdout, 1, 1, 3, ' ', 0)
|
|
|
|
// Print the header row (terminate columns with \t and rows with \n)
|
|
fmt.Fprintln(w, "ID\tNAME\tROLE\tSTATUS\t")
|
|
|
|
// Print data rows
|
|
fmt.Fprintln(w, "1\tAlice Vance\tDeveloper\tActive\t")
|
|
fmt.Fprintln(w, "2\tBob Miller\tManager\tAway\t")
|
|
fmt.Fprintln(w, "105\tCharlie\tDesigner\tOffline\t")
|
|
|
|
// Flush to format and output everything to the console
|
|
w.Flush()
|
|
}
|
|
|
|
```
|
|
|
|
for env file parsing
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func main() {
|
|
// Loads .env file into os.Environ()
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file")
|
|
}
|
|
}
|
|
```
|
|
|
|
or without external library
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func loadEnvFile(path string) error {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
// skip empty lines and comments
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
|
|
// strip optional "export " prefix
|
|
line = strings.TrimPrefix(line, "export ")
|
|
|
|
// for inline comments
|
|
if strings.HasPrefix(line, `"`) || strings.HasPrefix(line, `'`) {
|
|
// quoted — keep everything between the quotes
|
|
line = strings.Trim(line, `"'`)
|
|
} else {
|
|
// unquoted — cut at first " #" (space + hash)
|
|
if idx := strings.Index(line, " #"); idx != -1 {
|
|
line = strings.TrimSpace(line[:idx])
|
|
}
|
|
}
|
|
|
|
|
|
key, value, found := strings.Cut(line, "=")
|
|
if !found {
|
|
continue
|
|
}
|
|
|
|
key = strings.TrimSpace(key)
|
|
value = strings.TrimSpace(value)
|
|
|
|
// strip surrounding quotes
|
|
value = strings.Trim(value, `"'`)
|
|
|
|
os.Setenv(key, value)
|
|
}
|
|
return scanner.Err()
|
|
}
|
|
|
|
func main() {
|
|
if err := loadEnvFile(".env"); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(os.Getenv("DB_HOST"))
|
|
}
|
|
```
|
|
|
|
|
|
for timestamps
|
|
|
|
```go
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
unixTimestamp := int64(1615460256)
|
|
t := time.Unix(unixTimestamp, 0)
|
|
isoString := t.Format(time.RFC3339)
|
|
// or for readibility
|
|
// timeISOString := time.Time.Local(t).String()
|
|
// or for gmt
|
|
// timeISOString := time.Time.UTC(t).Format(http.TimeFormat)
|
|
fmt.Println(isoString) // Output: 2021-03-11T06:17:36Z
|
|
}
|
|
```
|
|
|
|
for milli sec one
|
|
|
|
```go
|
|
unixMillis := int64(1615460256000)
|
|
t := time.Unix(unixMillis/1000, (unixMillis%1000)*int64(time.Millisecond))
|
|
fmt.Println(t.Format(time.RFC3339))
|
|
```
|
|
|