github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/callgraph/vta/testdata/src/select.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // go:build ignore
     6  
     7  package testdata
     8  
     9  type I interface {
    10  	Foo() string
    11  }
    12  
    13  type J interface {
    14  	I
    15  }
    16  
    17  type B struct {
    18  	p string
    19  }
    20  
    21  func (b B) Foo() string { return b.p }
    22  
    23  func Baz(b1, b2 B, c1 chan I, c2 chan J) {
    24  	for {
    25  		select {
    26  		case c1 <- b1:
    27  			print("b1")
    28  		case c2 <- b2:
    29  			print("b2")
    30  		case <-c1:
    31  			print("c1")
    32  		case k := <-c2:
    33  			print(k.Foo())
    34  			return
    35  		}
    36  	}
    37  }
    38  
    39  // Relevant SSA:
    40  // func Baz(b1 B, b2 B, c1 chan I, c2 chan J):
    41  //   ...
    42  //   t2 = *t0
    43  //   t3 = make I <- B (t2)
    44  //   t4 = *t1
    45  //   t5 = make J <- B (t4)
    46  //   t6 = select blocking [c1<-t3, c2<-t5, <-c1, <-c2] (index int, ok bool, I, J)
    47  //   t7 = extract t6 #0
    48  //   t8 = t7 == 0:int
    49  //   if t8 goto 2 else 3
    50  //         ...
    51  //  8:
    52  //   t15 = extract t6 #3
    53  //   t16 = invoke t15.Foo()
    54  //   t17 = print(t18)
    55  
    56  // WANT:
    57  // Local(t3) -> Channel(chan testdata.I)
    58  // Local(t5) -> Channel(chan testdata.J)
    59  // Channel(chan testdata.I) -> Local(t6[2])
    60  // Channel(chan testdata.J) -> Local(t6[3])