| // run |
| |
| // Copyright 2020 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 |
| |
| import "fmt" |
| |
| type Value[T any] struct { |
| val T |
| } |
| |
| func (v *Value[T]) Get() T { |
| return v.val |
| } |
| |
| func (v *Value[T]) Set(val T) { |
| v.val = val |
| } |
| |
| func main() { |
| var v1 Value[int] |
| v1.Set(1) |
| if got, want := v1.Get(), 1; got != want { |
| panic(fmt.Sprintf("Get() == %d, want %d", got, want)) |
| } |
| v1.Set(2) |
| if got, want := v1.Get(), 2; got != want { |
| panic(fmt.Sprintf("Get() == %d, want %d", got, want)) |
| } |
| |
| var v2 Value[string] |
| v2.Set("a") |
| if got, want := v2.Get(), "a"; got != want { |
| panic(fmt.Sprintf("Get() == %q, want %q", got, want)) |
| } |
| } |