function61 / gokit Goto Github PK
View Code? Open in Web Editor NEWFrequently needed, stupid simple, packages in Go
Home Page: https://function61.com/
License: Apache License 2.0
Frequently needed, stupid simple, packages in Go
Home Page: https://function61.com/
License: Apache License 2.0
Most used ones:
Contains()
Map()
Filter()
Others:
Some()
?Wrong repo :D
var (
// can't patch existing HTTP client because we could end up modifying `http.DefaultClient`
// https://stackoverflow.com/a/38150816
noFollowRedirects = ezhttp.Client(&http.Client{
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
// no error - just don't follow redirects
return http.ErrUseLastResponse
},
})
)
Some sketching:
type Result[T any] struct {
item *T
err error
}
func Ok[T any](value T) Result[T] {
return Result[T]{
item: &value,
err: nil,
}
}
func Err[T any](err error) Result[T] {
return Result[T]{
item: nil,
err: err,
}
}
func getTemp(succeed bool) Result[string] {
if succeed {
return Ok("warm")
} else {
return Err[string](errors.New("could not succeed"))
}
}
func main() {
temp := getTemp(true)
fmt.Printf("temp=%s\n", *temp.item)
}
Prototype:
// 'ternary operator'
func IfElseAssign[T any](val bool, ifTrue T, ifFalse T) T {
if val {
return ifTrue
} else {
return ifFalse
}
}
// 'ternary operator' (lazy production of value)
func IfElseAssignFn[T any](val bool, ifTrue func() T, ifFalse func() T) T {
if val {
return ifTrue()
} else {
return ifFalse()
}
}
// "adapter" for making values for `IfElseAssignFn` without having to type the full `func () string { return "foo" }`
func fn[T any](val T) func() T {
return func() T { return val }
}
func main() {
name := "Joonas"
powerEstimate := IfElseAssignFn(name == "Joonas", fn(9001), fn(3))
powerEstimate = IfElseAssign(name == "Joonas", 9001, 3)
fmt.Printf("Power of %s = %d\n", name, powerEstimate)
}
Shouldn't be in root, but moving requires change to https://github.com/function61/buildkit-golang
Also, investigate if https://golang.org/cmd/buildid/ has overlap?
There is no support for the doas
command, which is a secure alternative to sudo
.
opendoas sets the DOAS_USER
environment variables (e.g. DOAS_USER="humaid"
).
An alternate check should be added somewhere here, but the UID has to be fetched.
gokit/os/user/userutil/privileges_linux.go
Line 226 in 27b5c49
This makes it more versatile
Implement it with assert.Equal(t, expr, true)
?
If two processes are racing to make the same final file:
rename /run/notifydedup/counter.json.part /run/notifydedup/counter.json: no such file or directory; also failed removing temp file: remove /run/notifydedup/counter.json.part: no such file or directory
Think timeline:
counter.json.part
counter.json.part
, overriding first oneIs it possible to use locking with the part file in a way that two competing processes are not racing to produce the target file?
Playground code:
package main
import (
"encoding/json"
"fmt"
"io"
"strings"
)
// (ab)using the fact that the JSON decoder has accumulated over all it's read plus it's working on the new buffer
// it's about to receive now.
type lineCounter struct {
committedLines int
committedBytes int64
uncommitted []byte
uncommittedLines int
}
func (l *lineCounter) LineNumberFromOffset(offset int64) int {
if offset < l.committedBytes {
panic("offset < l.committedBytes")
}
intoUncommitted := offset - l.committedBytes
linesIntoUncommitted := countNewlines(l.uncommitted[:intoUncommitted])
return l.committedLines + linesIntoUncommitted
}
var _ io.Writer = (*lineCounter)(nil)
func (l *lineCounter) Write(buf []byte) (int, error) {
// the fact that `Write()` was called means that the previous uncommitted buffer was consumed fully. "commit" now.
l.committedBytes += int64(len(l.uncommitted))
l.committedLines += l.uncommittedLines
l.uncommittedLines = countNewlines(buf)
l.uncommitted = buf
return len(buf), nil
}
func main() {
lc := &lineCounter{}
jd := json.NewDecoder(io.TeeReader(strings.NewReader(`{
"Foo": "bar"}`), lc))
err := jd.Decode(&struct{ Foo struct{} }{})
if err != nil {
fmt.Printf("err=%v pos=%d\n", err, lc.LineNumberFromOffset(jd.InputOffset()))
}
fmt.Println("Hello, 世界")
}
func countNewlines(buf []byte) int {
newlines := 0
for _, ch := range buf {
if ch == '\n' {
newlines++
}
}
return newlines
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.