golang.org/x/tools@v0.21.0/internal/refactor/inline/testdata/import-shadow.txtar (about)

     1  Just because a package (e.g. log) is imported by the caller,
     2  and the name log is in scope, doesn't mean the name in scope
     3  refers to the package: it could be locally shadowed.
     4  
     5  In all three scenarios below, renaming import with a fresh name is
     6  added because the usual name is locally shadowed: in cases 1, 2 an
     7  existing import is shadowed by (respectively) a local constant,
     8  parameter; in case 3 there is no existing import.
     9  
    10  -- go.mod --
    11  module testdata
    12  go 1.12
    13  
    14  -- a/a.go --
    15  package a
    16  
    17  import "testdata/b"
    18  import "log"
    19  
    20  func A() {
    21  	const log = "shadow"
    22  	b.B() //@ inline(re"B", bresult)
    23  }
    24  
    25  var _ log.Logger
    26  
    27  -- b/b.go --
    28  package b
    29  
    30  import "log"
    31  
    32  func B() {
    33  	log.Printf("")
    34  }
    35  
    36  -- bresult --
    37  package a
    38  
    39  import (
    40  	"log"
    41  	log0 "log"
    42  )
    43  
    44  func A() {
    45  	const log = "shadow"
    46  	log0.Printf("") //@ inline(re"B", bresult)
    47  }
    48  
    49  var _ log.Logger
    50  
    51  -- go.mod --
    52  module testdata
    53  go 1.12
    54  
    55  -- a/a.go --
    56  package a
    57  
    58  import "testdata/b"
    59  
    60  var x b.T
    61  
    62  func A(b int) {
    63  	x.F() //@ inline(re"F", fresult)
    64  }
    65  
    66  -- b/b.go --
    67  package b
    68  
    69  type T struct{}
    70  
    71  func (T) F() {
    72  	One()
    73  	Two()
    74  }
    75  
    76  func One() {}
    77  func Two() {}
    78  
    79  -- fresult --
    80  package a
    81  
    82  import (
    83  	"testdata/b"
    84  	b0 "testdata/b"
    85  )
    86  
    87  var x b.T
    88  
    89  func A(b int) {
    90  	b0.One()
    91  	b0.Two() //@ inline(re"F", fresult)
    92  }
    93  
    94  -- d/d.go --
    95  package d
    96  
    97  import "testdata/e"
    98  
    99  func D() {
   100  	const log = "shadow"
   101  	e.E() //@ inline(re"E", eresult)
   102  }
   103  
   104  -- e/e.go --
   105  package e
   106  
   107  import "log"
   108  
   109  func E() {
   110  	log.Printf("")
   111  }
   112  
   113  -- eresult --
   114  package d
   115  
   116  import (
   117  	log0 "log"
   118  )
   119  
   120  func D() {
   121  	const log = "shadow"
   122  	log0.Printf("") //@ inline(re"E", eresult)
   123  }