Skip to content

Commit

Permalink
go mod init simonwaldherr.de/go/golibs
Browse files Browse the repository at this point in the history
go mod tidy
  • Loading branch information
SimonWaldherr committed Apr 17, 2021
1 parent 5d72bfa commit 15bb8aa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
23 changes: 21 additions & 2 deletions arg/arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ type argument struct {
flagValue *string
usage string
timeout time.Duration
argNr int
}

var flagVar = make(map[string]argument)
var values = make(map[string]interface{})
var i = 1

func String(name, defaultValue, usage string, timeout time.Duration) {
flagVar[name] = argument{
Expand All @@ -27,16 +29,31 @@ func String(name, defaultValue, usage string, timeout time.Duration) {
flagValue: flag.String(name, "", usage),
usage: usage,
timeout: timeout,
argNr: -1,
}
}

func StringArg(name, defaultValue, usage string, timeout time.Duration) {
flagVar[name] = argument{
name: name,
defaultValue: defaultValue,
flagValue: flag.String(name, "", usage),
usage: usage,
timeout: timeout,
argNr: i,
}
i++
}

func Parse() {
var value interface{}

flag.Parse()

for v := range flagVar {
if *flagVar[v].flagValue == "" {
if *flagVar[v].flagValue != "" {
value = *flagVar[v].flagValue
} else if !(flagVar[v].argNr >= 1 && flagVar[v].argNr < len(os.Args)) && flagVar[v].timeout >= time.Second*1 {
fmt.Printf("# %v: ", flagVar[v].usage)
scanner := bufio.NewScanner(os.Stdin)
ch := make(chan bool, 1)
Expand All @@ -52,8 +69,10 @@ func Parse() {
case <-time.After(flagVar[v].timeout):
value = flagVar[v].defaultValue
}
} else if (flagVar[v].argNr >= 1 && flagVar[v].argNr < len(os.Args)) {
value = os.Args[flagVar[v].argNr]
} else {
value = *flagVar[v].flagValue
value = flagVar[v].defaultValue
}
values[v] = value
}
Expand Down
17 changes: 17 additions & 0 deletions arg/demo/arg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"simonwaldherr.de/go/golibs/arg"
"time"
)

func main() {
arg.StringArg("name1", "default", "enter string", time.Second*5)
arg.StringArg("name2", "default", "enter string", time.Second*5)
arg.StringArg("name3", "default", "enter string", time.Second*0)
arg.Parse()
fmt.Println("\ninput 1:", arg.Get("name1"))
fmt.Println("\ninput 2:", arg.Get("name2"))
fmt.Println("\ninput 3:", arg.Get("name3"))
}
2 changes: 1 addition & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (cache *Cache) Import(r io.Reader) error {
if err := dec.Decode(&X); err != nil {
return err
}

for k, v := range X {
cache.Set(k, v)
}
Expand Down
6 changes: 3 additions & 3 deletions cache/example_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cache_test

import (
"bytes"
"fmt"
"simonwaldherr.de/go/golibs/cache"
"time"
"bytes"
)

func ExampleCache() {
Expand Down Expand Up @@ -44,7 +44,7 @@ func ExampleExport() {
c2.Import(&buf)

fmt.Println(c2.String())

// Output:
//0 23
//2 foobar
Expand All @@ -55,4 +55,4 @@ func ExampleExport() {
//2 foobar
//4 false
//8 [116 101 115 116]
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module simonwaldherr.de/go/golibs

// replace simonwaldherr.de/go/golibs => /Users/simonwaldherr/git/golibs

go 1.16
3 changes: 0 additions & 3 deletions xmath/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ func Rootmeansquare(val interface{}) float64 {
}

// Harmonic returns the harmonic mean from a slice of Values as float64.
// The arithmetic mean or simply the mean or average when the context is clear,
// is the sum of a list of numbers divided by the number of numbers
// in the list.
// It uses "as" (simonwaldherr.de/go/golibs/as) to
// convert given values to floats.
func Harmonic(val interface{}) float64 {
Expand Down

0 comments on commit 15bb8aa

Please sign in to comment.