golibs
golang functions (to be included in other projects)
go get -u -t simonwaldherr.de/go/golibs/...
coverage & tests
go test ./...
each new build gets tested in multiple steps:
- on development i regularly type
go test
from time to time to check the test suite - also there are a few go apps in the examples-folder which i test to build and run
- on commit, git automatically runs the pre-commit-hook shell script
- after a commit gets pushed to GitHub, the following tests are started via Webhooks and Services
- Travis CI build the lib and all tests on docker containers with the go versions noted in .travis.yml
- semaphoreci, yet another (linux (ubuntu) based) ci service
ToC
- ansi can print colored and styled text to your terminal
- arg simplifies cli flags (arguments)
- as can convert most standard data types to most other data types
- bitmask set and get bits in a bitmask
- cache is an easy and small caching package
- cachedfile simplifies reading and writing from and to disk and adds caching
- channel simplifies channel operations, e.g. sending the same data to multiple receivers
- csv load and parse CSV file
- file wraps around the standard functions to simplify reading and writing on disk
- foreach calls a given function for each element of a [ JSON-string ]
- gcurses enabling the development of text user interface applications
- gopath provides an easy way to get system information
- graphics can manipulate images
- http make easy and fast HTTP(S) requests
- log helps on logging your applications status
- re helps you whenever you have to do something multiple times
- regex is a layer to speed up your regular expression development
- rss is a rss feed parser based on Golangs std xml package
- ssl generates ssl certificates for https
- stack can store your values in stacks and rings
- structs use structs like maps
- xmath provides a few mathematical functions like Sum, Median, Harmonic-mean, …
- xtime xtime implements a subset of strftime
WARNING
some functions in this repo enables you to write really bad code - I wrote it anyway because:
- sometimes you need a quick and dirty solution
- it shows you possible ways, feel free to fork and customize
- mostly bad go code is better than good php code - every aspect in golang is designed to prevent from bad code
BUT: please remember that you can do things more performantly e.g. converting a string to a float is much faster done with strconv.ParseFloat than with as.Float.
sublibs
ansi -
import "simonwaldherr.de/go/golibs/ansi"
ansi can print colored and styled text to your terminal:
- green, yellow and red strings:
log.Println(ansi.Color("INFO: everything is fine", ansi.Green))
log.Println(ansi.Color("WARNING: not everything is fine", ansi.Yellow))
log.Println(ansi.Color("ERROR: OMG!!!", ansi.Red))
- bold and underlined text:
fmt.Printf("this is %v and %v text", ansi.Bold("bold"), ansi.Underline("underlined"))
arg -
import "simonwaldherr.de/go/golibs/arg"
the arg package simplifies cli flags (arguments)
as -
import "simonwaldherr.de/go/golibs/as"
with as you can convert most standard data types to most other data types e.g.
- int to string:
var x string = as.String(int(32))
- string to int:
var x int = as.Int("32")
- string to time:
var x time.Time = as.Time("31.12.2014")
bitmask -
import "simonwaldherr.de/go/golibs/bitmask"
with bitmask you can set and get bits to and from a bitmask:
i := bitmask.New(0b11111111)
i.Set(0, false)
cache -
import "simonwaldherr.de/go/golibs/cache"
cachedfile -
import "simonwaldherr.de/go/golibs/cachedfile"
cachedfile simplifies reading and writing from and to disk and adds caching
do
str := "Neque porro quisquam est, qui dolorem ipsum, quia dolor sit, amet, consectetur, adipisci velit."
err := cachedfile.Write("filename.txt", str, false)
and in less then 15 minutes this
str, _ := cachedfile.Read("filename.txt")
and there will be no file access to disk.
If you kill the App, wait 15 min or call cachedfile.Stop()
the cached content will be exported to disk.
channel -
import "simonwaldherr.de/go/golibs/channel"
channel simplifies channel operations, e.g. sending the same data to multiple receivers
con := channel.Init()
receiver01 := con.AddReceiver()
receiver02 := con.AddReceiver()
receiver03 := con.AddReceiver()
go func() {
fmt.Println(<-receiver01)
}()
go func() {
fmt.Println(<-receiver02)
}()
go func() {
fmt.Println(<-receiver03)
}()
transmitter01 := con.AddTransmitter()
transmitter01 <- "Hello World"
csv -
import "simonwaldherr.de/go/golibs/csv"
file -
import "simonwaldherr.de/go/golibs/file"
file wraps around the standard functions to simplify reading and writing on disk
str := "Neque porro quisquam est, qui dolorem ipsum, quia dolor sit, amet, consectetur, adipisci velit."
err := file.Write("filename.txt", str, false)
Besides simple reading and writing, the package also contains functions to test file statuses, read large files by small blocks, clear, rename and delete files. There is even a function for do things with each file in a directory (even with subfolders):
err := file.Each("..", true, func(filename, extension, filepath string, dir bool, fileinfo os.FileInfo) {
if extension == "go" && !dir {
t.Logf("%v, %v, %v, %v\n", filename, filepath, dir, fileinfo)
}
}
If you need the absolute path to a file, but only have a relative path, you can use file.GetAbsolutePath("~/path/to/file.txt")
.
foreach -
import "simonwaldherr.de/go/golibs/foreach"
foreach calls a given function for each element of a [ JSON-string ]
gcurses -
import "simonwaldherr.de/go/golibs/gcurses"
gcurses is a terminal control library for Unixoide systems, enabling the development of text user interface applications.
Named after the "GUI-like" terminal application toolkit ncurses (new curses) which is named after the original curses. This library is in a very early stage.
package main
import (
"fmt"
"simonwaldherr.de/go/golibs/gcurses"
"time"
)
func main() {
writer := gcurses.New()
writer.Start()
for i := 0; i < 100; i++ {
fmt.Fprintf(writer, "Count till one hundred: %d\nStill counting ...\n", i)
time.Sleep(time.Millisecond * 10)
}
time.Sleep(time.Millisecond * 500)
fmt.Fprintln(writer, "Finished counting")
writer.Stop()
}
gopath -
import "simonwaldherr.de/go/golibs/gopath"
gopath provides an easy way to get system information
to read a config file in which is in the same file as the executable, you can do something like this:
package main
import (
"fmt"
"path/filepath"
"simonwaldherr.de/go/golibs/file"
"simonwaldherr.de/go/golibs/gopath"
)
func main() {
dir := gopath.Dir()
config := file.Read(filepath.Join(dir, "config.yaml"))
...
graphics -
import "simonwaldherr.de/go/golibs/graphics"
with graphics you can manipulate images
img := graphics.EachPixel(file, func(r, g, b, a uint8) (uint8, uint8, uint8, uint8) {
return g, b, r, a
})
you even can apply filters to images:
file, _ := os.Open("./original.png")
defer file.Close()
img, _, err := image.Decode(file)
img = Edgedetect(img)
out, _ := os.Create("./edgeDetect.png")
png.Encode(out, img)
fd.Close()
http -
import "simonwaldherr.de/go/golibs/http"
log -
import "simonwaldherr.de/go/golibs/log"
re -
import "simonwaldherr.de/go/golibs/re"
re helps you whenever you have to do something multiple times
data, stop := re.Do(time.Second * 5, func(data chan<- interface{}) {
data <- fmt.Sprintf("%v\n", time.Now().Format("02.01.2006 15:04:05"))
})
regex -
import "simonwaldherr.de/go/golibs/regex"
regex is a layer to speed up your regular expression development
str, err := regex.ReplaceAllString("Ipsum Lorem", "([^ ]+) ([^ ]+)", "$2 $1")
rss -
import "simonwaldherr.de/go/golibs/rss"
rss is a rss feed parser based on Golangs std xml package
podcast, err := rss.Read(url)
if err == nil {
for _, episode := range podcast.Items {
fmt.Println(episode.Title)
}
}
ssl -
import "simonwaldherr.de/go/golibs/ssl"
ssl generates ssl certificates for https
err := ssl.Generate(options)
stack -
import "simonwaldherr.de/go/golibs/stack"
with stack you can store your values in stacks and rings
array := stack.Lifo()
array.Push(as.Bytes(12.34))
array.Push(as.Float(13.37))
array.Push(as.String(23.0))
for array.Len() > 0 {
log.Println(array.Pop())
}
structs -
import "simonwaldherr.de/go/golibs/structs"
xmath -
import "simonwaldherr.de/go/golibs/xmath"
xmath provides a few mathematical functions like Max, Min, Sum, Median, Harmonic-mean, ...
var f = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.000003}
fmt.Printf("Max: %v\n", xmath.Max(f))
fmt.Printf("Min: %v\n", xmath.Min(f))
fmt.Printf("Sum: %v\n", xmath.Sum(f))
fmt.Printf("Median: %v\n", xmath.Median(f))
fmt.Printf("Arithmetic: %v\n", xmath.Arithmetic(f))
fmt.Printf("Harmonic: %v\n", xmath.Harmonic(f))
fmt.Printf("Geometric: %v\n", xmath.Geometric(f))
xtime -
import "simonwaldherr.de/go/golibs/xtime"
xtime implements a subset of strftime
t, _ := time.Now()
fmt.Println(xtime.Fmt("%Y-%m-%d %H:%M:%S", t))