codekingpro/portable-devtools
114k
1// run2 3// Copyright 2021 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7// Test for declaration and use of a parameterized embedded field.8 9package main10 11import (12 "fmt"13 "sync"14)15 16type MyStruct[T any] struct {17 val T18}19 20type Lockable[T any] struct {21 MyStruct[T]22 mu sync.Mutex23}24 25// Get returns the value stored in a Lockable.26func (l *Lockable[T]) Get() T {27 l.mu.Lock()28 defer l.mu.Unlock()29 return l.MyStruct.val30}31 32// Set sets the value in a Lockable.33func (l *Lockable[T]) Set(v T) {34 l.mu.Lock()35 defer l.mu.Unlock()36 l.MyStruct = MyStruct[T]{v}37}38 39func main() {40 var li Lockable[int]41 42 li.Set(5)43 if got, want := li.Get(), 5; got != want {44 panic(fmt.Sprintf("got %d, want %d", got, want))45 }46}47 