Skip to content

Commit fbfec4d

Browse files
committed
Update lib
1 parent 364a3e4 commit fbfec4d

File tree

14 files changed

+81
-43
lines changed

14 files changed

+81
-43
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/mohanson/daze
22

3-
go 1.23
3+
go 1.25

lib/doa/doa.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
// Package doa stands for "dead or alive". It provides simple utilities to intentionally crash the program with a panic.
22
package doa
33

4+
import (
5+
"log"
6+
)
7+
48
// Doa checks a boolean condition and triggers a panic if it’s false.
59
func 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.
1721
func 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.
2428
func 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
}

lib/gracefulexit/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
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.

lib/gracefulexit/cmd/http/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

lib/gracefulexit/gracefulexit.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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.
45
package gracefulexit
56

67
import (

lib/lru/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Lru
22

3-
Package lru implements an lru cache.
3+
Package lru implements an LRU cache.

lib/lru/lru.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// A data structure that follows the constraints of a least recently used (lru) cache.
1+
// Package lru implements an LRU cache.
22
package lru
33

44
import (
@@ -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.
7586
func (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.
105105
func (l *Lru[K, V]) Has(k K) bool {
106106
l.M.Lock()

lib/pretty/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
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

1010
2025/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

2018
2025/03/12 09:51:57 pretty: City name Area Population Annual Rainfall

lib/pretty/pretty.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
// Package pretty provides utilities for beautifying console output.
12
package pretty
23

34
import (
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.
1312
func 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 {

lib/priority/cmd/race/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)