https://github.com/codecat/go-libs
Collection of my Go libraries.
https://github.com/codecat/go-libs
Last synced: 11 months ago
JSON representation
Collection of my Go libraries.
- Host: GitHub
- URL: https://github.com/codecat/go-libs
- Owner: codecat
- License: mit
- Created: 2017-06-18T13:43:51.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-09-06T17:46:58.000Z (over 4 years ago)
- Last Synced: 2024-12-29T18:38:05.654Z (over 1 year ago)
- Language: Go
- Size: 27.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Funding: .github/FUNDING.yml
- License: License.txt
Awesome Lists containing this project
README
# Codecat Go libs
[](https://travis-ci.org/codecat/go-libs) [](https://goreportcard.com/report/github.com/codecat/go-libs)
These are some of the smaller Go helper libraries I've written that I use in my Go applications.
To use these, install them using `go get`, for example:
```
$ go get github.com/codecat/go-libs/log
$ go get github.com/codecat/go-libs/pacman
$ go get github.com/codecat/go-libs/settings
```
## Libraries
### `log`
Call any of these to actually write to the log:
* `log.Trace(format string, args ...interface{})`
* `log.Debug(format string, args ...interface{})`
* `log.Info(format string, args ...interface{})`
* `log.Warn(format string, args ...interface{})`
* `log.Error(format string, args ...interface{})`
* `log.Fatal(format string, args ...interface{})`
### `pacman`
Binary packing streams.
* To pack data directly to a file:
```go
file, _ := os.Create("test.bin")
packer, _ := pacman.NewPacker(file)
```
* To create a packer for a memory stream:
```go
buffer := new(bytes.Buffer)
packer, _ := pacman.NewPacker(buffer)
packer.WriteUInt32(0)
```
* To unpack data from a file:
```go
file, _ := os.Open("test.bin")
unpacker, _ := pacman.NewUnpacker(file)
```
* To unpack data from a byte slice:
```go
buffer := bytes.NewBuffer(slice)
unpacker, _ := pacman.NewUnpacker(buffer)
```
Also has support for "blocks", which is useful if you want to be able to skip blocks for whatever reason.
```go
packer, _ := pacman.NewPacker(buffer)
b := packer.BeginBlock()
b.WriteInt32(1)
b.WriteInt32(2)
b.WriteInt32(3)
packer.EndBlock()
packer.WriteInt32(4)
```
```go
unpacker, _ := pacman.NewUnpacker(buffer)
b := unpacker.ReadBlock()
x := b.ReadInt32() // 1
y := b.ReadInt32() // 2
// We can ignore (or read later) the 3rd integer in the block
unpacker.ReadInt32() // 4
z := b.ReadInt32() // 3
```
### `settings`
Load and save a settings yaml file.
* Instantiate a structure:
```go
var config struct {
Foo string
Bar string
}
```
* Call `settings.Load(filename string, out interface{})` on the structure:
```go
func main() {
err := settings.Load("config.yaml", &config)
if err != nil {
fmt.Println(err.Error())
}
}
```
* After modifying programmatically, save using `settings.Save(filename string, in interface{})`:
```go
err := settings.Save("config.yaml", &config)
if err != nil {
fmt.Println(err.Error())
}
```