https://github.com/bahlo/abutil
[UNMAINTAINED] :ab: A collection of often-used Golang helpers
https://github.com/bahlo/abutil
Last synced: 10 months ago
JSON representation
[UNMAINTAINED] :ab: A collection of often-used Golang helpers
- Host: GitHub
- URL: https://github.com/bahlo/abutil
- Owner: bahlo
- License: wtfpl
- Archived: true
- Created: 2015-08-26T09:14:19.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-09-02T12:54:24.000Z (over 10 years ago)
- Last Synced: 2024-11-16T23:32:32.328Z (over 1 year ago)
- Language: Go
- Homepage: https://godoc.org/github.com/bahlo/abutil
- Size: 664 KB
- Stars: 54
- Watchers: 5
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - abutil - [UNMAINTAINED] A collection of often-used Golang helpers - ★ 48 (Utilities)
- awesome-go-cn - abutil - used Golang helpers.) (实用工具 / Advanced Console UIs)
- awesome-go - abutil - Collection of often-used Golang helpers. - :arrow_down:0 - :star:9 (Utilities / HTTP Clients)
- awesome-go-processed - abutil - Collection of often-used Golang helpers.| (Utilities / Advanced Console UIs)
- awesome-go - abutil - Collection of often-used Golang helpers. (Utilities / Advanced Console UIs)
- awesome-go - abutil - used Golang helpers. | - | - | - | (Utilities / HTTP Clients)
- fucking-awesome-go - :octocat: abutil - A collection of often-used Golang helpers. :star: 6 :fork_and_knife: 0 (Utilities / Advanced Console UIs)
- awesome-go-zh - abutil
- awesome-go - abutil - Collection of often-used Golang helpers. (Utilities / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
README
# abutil [](https://godoc.org/github.com/bahlo/abutil) [](https://travis-ci.org/bahlo/abutil) [](https://coveralls.io/github/bahlo/abutil?branch=master)
abutil is a collection of often-used Golang helpers.
## Contents
- [Functions](#functions)
- [OnSignal](#onsignal)
- [Parallel](#parallel)
- [RollbackErr](#rollbackerr)
- [RemoteIP](#remoteip)
- [GracefulServer](#gracefulserver)
- [License](#license)
## Functions
#### [OnSignal](https://godoc.org/github.com/bahlo/abutil#OnSignal)
Listens to various signals and executes the given function with the received
signal.
```go
go abutil.OnSignal(func(s os.Signal) {
fmt.Printf("Got signal %s\n", s)
})
```
#### [Parallel](https://godoc.org/github.com/bahlo/abutil#Parallel)
Executes the given function n times concurrently.
```go
var m sync.Mutex
c := 0
abutil.Parallel(4, func() {
m.Lock()
defer m.Unlock()
fmt.Print(c)
c++
})
```
#### [RollbackErr](https://godoc.org/github.com/bahlo/abutil#RollbackErr)
Does a rollback on the given transaction and returns either the rollback-error,
if occured, or the given one.
```go
insertSomething := func(db *sql.DB) error {
tx, _ := db.Begin()
_, err := tx.Exec("INSERT INTO some_table (some_column) VALUES (?)",
"foobar")
if err != nil {
// The old way, imagine doing this 10 times in a method
if err := tx.Rollback(); err != nil {
return err
}
return err
}
_, err = tx.Exec("DROP DATABASE foobar")
if err != nil {
// With RollbackErr
return abutil.RollbackErr(tx, err)
}
return nil
}
```
#### [RemoteIP](https://godoc.org/github.com/bahlo/abutil#RemoteIP)
Tries everything to get the remote ip.
```go
someHandler := func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("New request from %s\n", abutil.RemoteIP(r))
}
```
#### [GracefulServer](https://godoc.org/github.com/bahlo/abutil#GracefulServer)
A wrapper around `graceful.Server` from
with state variable and easier handling.
```go
s := abutil.NewGracefulServer(1337, someHandlerFunc)
// This channel blocks until all connections are closed or the time is up
sc := s.StopChan()
// Some go func that stops the server after 2 seconds for no reason
time.After(2 * time.Second, func() {
s.Stop(10 * time.Second)
})
if err := s.ListenAndServe(); err != nil && !s.Stopped() {
// We didn't stop the server, so something must be wrong
panic(err)
}
// Wait for the server to finish
<-sc
```
## License
This project is licensed under the WTFPL, for more information see the LICENSE
file.