File tree Expand file tree Collapse file tree 14 files changed +81
-43
lines changed
Expand file tree Collapse file tree 14 files changed +81
-43
lines changed Original file line number Diff line number Diff line change 11module github.com/mohanson/daze
22
3- go 1.23
3+ go 1.25
Original file line number Diff line number Diff line change 11// Package doa stands for "dead or alive". It provides simple utilities to intentionally crash the program with a panic.
22package doa
33
4+ import (
5+ "log"
6+ )
7+
48// Doa checks a boolean condition and triggers a panic if it’s false.
59func Doa (b bool ) {
610 if ! b {
7- panic ( " unreachable" )
11+ log . Panicln ( "doa: unreachable" )
812 }
913}
1014
1115// Err returns the error passed to it, ignoring the first argument.
12- func Err [ T any ] (a T , err error ) error {
16+ func Err (a any , err error ) error {
1317 return err
1418}
1519
1620// Nil checks if an error is non-nil and panics if it is.
1721func Nil (err error ) {
1822 if err != nil {
19- panic ( err )
23+ log . Panicln ( "doa:" , err )
2024 }
2125}
2226
2327// Try returns a value if there’s no error, otherwise it panics.
2428func Try [T any ](a T , err error ) T {
2529 if err != nil {
26- panic ( err )
30+ log . Panicln ( "doa:" , err )
2731 }
2832 return a
2933}
Original file line number Diff line number Diff line change 11# Gracefulexit
22
3- A graceful exit (or graceful handling) is a simple programming idiom[ citation needed] wherein a program detects a serious error condition and "exits gracefully" in a controlled manner as a result. Often the program prints a descriptive error message to a terminal or log as part of the graceful exit.
3+ Package gracefulexit provides a method to exit the program gracefully. A graceful exit (or graceful handling) is a simple programming idiom[ citation needed] wherein a program detects a serious error condition and "exits gracefully" in a controlled manner as a result. Often the program prints a descriptive error message to a terminal or log as part of the graceful exit.
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "log"
5+ "net"
6+ "net/http"
7+
8+ "github.com/mohanson/daze/lib/gracefulexit"
9+ )
10+
11+ func main () {
12+ http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
13+ w .Write ([]byte ("Hello World!" ))
14+ w .Write ([]byte ("\n " ))
15+ })
16+ log .Println ("main: listen and server on 127.0.0.1:8080" )
17+ l , err := net .Listen ("tcp" , "127.0.0.1:8080" )
18+ if err != nil {
19+ log .Panicln ("main:" , err )
20+ }
21+ server := http.Server {}
22+ go server .Serve (l )
23+ gracefulexit .Wait ()
24+ log .Println ("main: server close" )
25+ server .Close ()
26+ log .Println ("main: done" )
27+ }
Original file line number Diff line number Diff line change 1- // A graceful exit (or graceful handling) is a simple programming idiom[citation needed] wherein a program detects a
2- // serious error condition and "exits gracefully" in a controlled manner as a result. Often the program prints a
3- // descriptive error message to a terminal or log as part of the graceful exit.
1+ // Package gracefulexit provides a method to exit the program gracefully. A graceful exit (or graceful handling) is a
2+ // simple programming idiom[citation needed] wherein a program detects a serious error condition and "exits gracefully"
3+ // in a controlled manner as a result. Often the program prints a descriptive error message to a terminal or log as part
4+ // of the graceful exit.
45package gracefulexit
56
67import (
Original file line number Diff line number Diff line change 11# Lru
22
3- Package lru implements an lru cache.
3+ Package lru implements an LRU cache.
Original file line number Diff line number Diff line change 1- // A data structure that follows the constraints of a least recently used (lru) cache.
1+ // Package lru implements an LRU cache.
22package lru
33
44import (
@@ -71,6 +71,17 @@ type Lru[K comparable, V any] struct {
7171 M * sync.Mutex
7272}
7373
74+ // Del removes the provided key from the cache.
75+ func (l * Lru [K , V ]) Del (k K ) {
76+ l .M .Lock ()
77+ defer l .M .Unlock ()
78+ if e , ok := l .C [k ]; ok {
79+ l .Drop (k , e .V )
80+ delete (l .C , k )
81+ l .List .Remove (e )
82+ }
83+ }
84+
7485// Get looks up a key's value from the cache.
7586func (l * Lru [K , V ]) GetExists (k K ) (v V , ok bool ) {
7687 l .M .Lock ()
@@ -90,17 +101,6 @@ func (l *Lru[K, V]) Get(k K) (v V) {
90101 return
91102}
92103
93- // Del removes the provided key from the cache.
94- func (l * Lru [K , V ]) Del (k K ) {
95- l .M .Lock ()
96- defer l .M .Unlock ()
97- if e , ok := l .C [k ]; ok {
98- l .Drop (k , e .V )
99- delete (l .C , k )
100- l .List .Remove (e )
101- }
102- }
103-
104104// Has returns true if a key exists.
105105func (l * Lru [K , V ]) Has (k K ) bool {
106106 l .M .Lock ()
Original file line number Diff line number Diff line change 11# Pretty
22
3- Utilities to prettify console output.
3+ Package pretty provides utilities for beautifying console output.
44
55** Progress**
66
7- ```
7+ ``` sh
88$ go run cmd/progress/main.go
99
10102025/03/12 09:53:42 pretty: [=========================> ] 59%
1111```
1212
13-
14-
1513** Table**
1614
17- ```
15+ ``` sh
1816$ go run cmd/table/main.go
1917
20182025/03/12 09:51:57 pretty: City name Area Population Annual Rainfall
Original file line number Diff line number Diff line change 1+ // Package pretty provides utilities for beautifying console output.
12package pretty
23
34import (
45 "fmt"
56 "log"
67 "os"
78 "strings"
8-
9- "github.com/mohanson/daze/lib/doa"
109)
1110
12- // PrintProgress draw a progress bar in the terminal. The percent takes values from 0 to 1.
11+ // PrintProgress draw a progress bar in the terminal. The percent takes values from 0 to 1.
1312func PrintProgress (percent float64 ) {
14- doa .Doa (0 <= percent && percent <= 1 )
13+ if percent < 0 || percent > 1 {
14+ log .Panicln ("pretty: the percent takes values from 0 to 1" )
15+ }
1516 out , _ := os .Stdout .Stat ()
1617 // Identify if we are displaying to a terminal or through a pipe or redirect.
1718 if out .Mode ()& os .ModeCharDevice == os .ModeCharDevice {
Original file line number Diff line number Diff line change @@ -38,6 +38,6 @@ func main() {
3838 <- ctx .Done ()
3939 fin ()
4040 for i := range cPriorityLevels {
41- log .Println ("main:" , i , ret [i ])
41+ log .Printf ("main: report level=%d count=%d " , i , ret [i ])
4242 }
4343}
You can’t perform that action at this time.
0 commit comments