FAQ

On a related note, I'm also confused about the costs of a slice of
interface type vs a slice of concrete type. Consider my
ASTFunctionCall above, which includes

args []ASTExpression

ASTExpression is an interface type: lots of AST nodes are expressions.
Currently I'm putting plain structs into my node lists, without
pointers:

// for arg list (op1+op2, "foo")
arg0 := ASTAdd{op1, op2}
arg1 := ASTString{"foo"}
args = []ASTExpression {arg0, arg1}

What exactly happens to that concrete ASTAdd struct when it's put into
the []ASTExpression slice-of-interfaces? Is the struct copied? Should
I use pointers instead to reduce copies?
Yes, the struct is copied. If you would have used a struct pointer in
C, you should probably use one in Go too. That will avoid the copy.
Whether it matters depends on how these get used, of course. But I
would probably use &ASTAdd{...} etc here and define the methods
satisfying ASTExpression to take pointer receivers to keep me honest.

http://research.swtch.com/interfaces has details about how interfaces
are implemented.

Russ

--

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 4 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 16, '12 at 5:08p
activeNov 25, '12 at 8:04p
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase