Comments (2)
That makes perfect sense @siebenmann, thanks for the great explanation.
I've added an exact reproduction of what you've described here.
Layout:
$ tree
.
├── A
│ └── lib.go
├── B
│ └── lib.go
├── C
│ └── lib.go
├── D
│ └── lib.go
└── main.go
As you've described:
A
declares a typeCalc
which implementsB.Adder
.
package A
type Calc struct{}
func (c *Calc) Add(a, b int32) int32 { return a + b }
B
declares anAdder
interface.
package B
type Adder interface{ Add(a, b int32) int32 }
C
implements a functionAdd
that instantiates aniface<B.Adder, *A.Calc>
internally:
package C
import (
"github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/A"
"github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/B"
)
func Add(a, b int32) int32 {
var adder B.Adder = &A.Calc{}
return adder.Add(a, b)
}
D
does the same thing asC
package D
import (
"github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/A"
"github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/B"
)
func Add(a, b int32) int32 {
var adder B.Adder = &A.Calc{}
return adder.Add(a, b)
}
- Finally, the
main package
callsC.Add
andD.Add
:
package main
import (
"fmt"
"github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/C"
"github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/D"
)
func main() {
fmt.Println(C.Add(10, 32))
fmt.Println(D.Add(10, 32))
}
Looking at the output of go build
:
$ go build -x
# ...
packagefile github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/C=$HOME/.cache/go-build/ff/ffc441d2cc7fd2bd9e12722f11fd3407dc4280577c0c74f8cb5241d72792d554-d
packagefile github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/D=$HOME/.cache/go-build/7c/7cc79061754b8fa877646bcbf5f10814217ac3dc48fc66c5964f08dd823695de-d
# ...
We can see the linker importing the archives for both package C
& D
, as expected.
Now if we look for itabs
in those archives:
$ go tool nm $HOME/.cache/go-build/ff/ffc441d2cc7fd2bd9e12722f11fd3407dc4280577c0c74f8cb5241d72792d554-d | grep 'itab\.'
af7 R go.itab.*github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/A.Calc,github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/B.Adder
$ go tool nm $HOME/.cache/go-build/7c/7cc79061754b8fa877646bcbf5f10814217ac3dc48fc66c5964f08dd823695de-d | grep 'itab\.'
af7 R go.itab.*github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/A.Calc,github.com/teh-cmc/go-internals/chapter2_interfaces/issue_7/B.Adder
We see that the itab
for iface<B.Adder, *A.Calc>
is indeed duplicated in both archives; so the linker will have to pick one.
from go-internals.
I've added a link to this discussion in the relevant part of chapter 2; and will be closing this now.
Don't hesitate to add more comments even if it's closed!
Hopefully I'll take the time to update chapter 2 with all these learnings some day.
from go-internals.
Related Issues (20)
- chapter2: about symbol addr and size HOT 1
- chapter2: Side issue: Spectre variant 2 HOT 1
- chapter1: Frame pointer HOT 9
- chapter1: about SP register HOT 7
- chapter1: digits of 137438953482 in base 2 HOT 1
- chapter1: objdump HOT 1
- meta: chinese translation? HOT 15
- meta: is this project still going on? HOT 4
- how did you gain such a strong understanding of go's internals? HOT 1
- chapter2: switch O(n) HOT 6
- good job, but when is GC part ? HOT 1
- chapter2: Interface conversions. Type assertions where the destination type is itself an interface.
- Clarify "top of the stack"
- chapter2 : interface Type-switches have sorted
- Arguments passing conventions HOT 2
- chapter1: Clarify "nop before call" paragraph HOT 4
- Request for update(if possible) HOT 1
- meta: set up gitbook HOT 2
- meta: talk about compiler internals? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-internals.