Skip to content

Commit f0ce2a6

Browse files
committed
builtin: add Append()
1 parent 62ba159 commit f0ce2a6

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

builtin/builtin.go

+6
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,9 @@ func Must[T any](value T, err error) T {
8080
func FirstError(errs ...error) error {
8181
return FirstNonEmpty(errs...)
8282
}
83+
84+
85+
// append to a slice without needing the "assign to same variable" boilerplate
86+
func Append[T any](slice *[]T, item T) {
87+
*slice = append(*slice, item)
88+
}

builtin/builtin_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package builtin
33
import (
44
"context"
55
"errors"
6+
"strings"
67
"testing"
78

89
"github.com/function61/gokit/testing/assert"
@@ -76,3 +77,12 @@ func TestFirstNonEmptyWithError(t *testing.T) {
7677
assert.EqualString(t, FirstNonEmpty(nilError, actualError).Error(), "actual")
7778
assert.Assert(t, FirstNonEmpty(nilError, nilError) == nil)
7879
}
80+
81+
func TestAppend(t *testing.T) {
82+
favoriteThings := []string{"hamburger"}
83+
84+
Append(&favoriteThings, "food")
85+
Append(&favoriteThings, "bar")
86+
87+
assert.EqualString(t, strings.Join(favoriteThings, ", "), "hamburger, food, bar")
88+
}

0 commit comments

Comments
 (0)