mirror of
https://github.com/golang/go
synced 2024-11-07 03:06:18 -07:00
c74be77e63
Similarly to what we do for the built-in function `copy`, where we allow a string as 2nd argument to append, also permit a type parameter constrained by string|[]byte. While at it, change date in the manual.go2 test files so that we don't need to constantly correct it when copying a test case from that file into a proper test file. Fixes #50281. Change-Id: I23fed66736aa07bb3c481fe97313e828425ac448 Reviewed-on: https://go-review.googlesource.com/c/go/+/376214 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
21 lines
415 B
Go
21 lines
415 B
Go
// run -gcflags=-G=3
|
|
|
|
// Copyright 2022 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
func add[S ~string | ~[]byte](buf *[]byte, s S) {
|
|
*buf = append(*buf, s...)
|
|
}
|
|
|
|
func main() {
|
|
var buf []byte
|
|
add(&buf, "foo")
|
|
add(&buf, []byte("bar"))
|
|
if string(buf) != "foobar" {
|
|
panic("got " + string(buf))
|
|
}
|
|
}
|