Skip to content

Commit

Permalink
doc: add cast and convert description
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 13, 2024
1 parent cf32e63 commit 7117b0a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ go get github.com/zeiss/pkg

Go has a pretty good standard library, but there are some things that are missing. This collection of small packages is meant to fill those gaps.

## Casting values

There is the typical case of pointers you need to deference. For example in the strict interfaces generated by [oapi-codegen](https://github.com/oapi-codegen/oapi-codegen).

```go
Expand All @@ -40,17 +42,26 @@ SomeStruct {
Return a default value of a `nil` pointer.

```go
type Foo struct {}
cast.Zero(Foo) // &Foo{}
```

func Value[T any](val *T) T {
if val == nil {
return Zero[T]()
}
## Converting values

return *val
}
There is also always the case to covert a value to a specific other value.

```go
// String converts a value to a string.
b := true
s := cast.String(b)

fmt.Println(s) // "true"
```

There are functions to convert `int`, `string` and `bool` values.

## Databases

There are also more complex tools like the `Database` interface which enables to easliy implement database wrappers.

```go
Expand Down

0 comments on commit 7117b0a

Please sign in to comment.