Skip to content

Commit

Permalink
builtin: add Append()
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Dec 19, 2024
1 parent 62ba159 commit f0ce2a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ func Must[T any](value T, err error) T {
func FirstError(errs ...error) error {
return FirstNonEmpty(errs...)
}


// append to a slice without needing the "assign to same variable" boilerplate
func Append[T any](slice *[]T, item T) {
*slice = append(*slice, item)
}
10 changes: 10 additions & 0 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package builtin
import (
"context"
"errors"
"strings"
"testing"

"github.com/function61/gokit/testing/assert"
Expand Down Expand Up @@ -76,3 +77,12 @@ func TestFirstNonEmptyWithError(t *testing.T) {
assert.EqualString(t, FirstNonEmpty(nilError, actualError).Error(), "actual")
assert.Assert(t, FirstNonEmpty(nilError, nilError) == nil)
}

func TestAppend(t *testing.T) {
favoriteThings := []string{"hamburger"}

Append(&favoriteThings, "food")
Append(&favoriteThings, "bar")

assert.EqualString(t, strings.Join(favoriteThings, ", "), "hamburger, food, bar")
}

0 comments on commit f0ce2a6

Please sign in to comment.