Debian Codegraph @

test/typeparam/genembed2.go

raw
1 // run
2
3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 // Test for declaration and use of a parameterized embedded field.
8
9 package main
10
11 import (
12 "fmt"
13 "sync"
14 )
15
16 type MyStruct[T any] struct {
17 val T
18 }
19
20 type Lockable[T any] struct {
21 MyStruct[T]
22 mu sync.Mutex
23 }
24
25 // Get returns the value stored in a Lockable.
26 func (l *Lockable[T]) Get() T {
27 l.mu.Lock()
28 defer l.mu.Unlock()
29 return l.MyStruct.val
30 }
31
32 // Set sets the value in a Lockable.
33 func (l *Lockable[T]) Set(v T) {
34 l.mu.Lock()
35 defer l.mu.Unlock()
36 l.MyStruct = MyStruct[T]{v}
37 }
38
39 func 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 }