I got curious about go a few weeks ago and decided to dive in and try
out a project. The result is a simple little graphics app you can see
here
http://obscure-texts.blogspot.com/2010/03/what-is-this-arclight-is-tool-for.html
I came away extremely impressed. Go walks an elegant line between
simplicity and power, and I already enjoy it more than languages I've
worked in for years. Naturally I have a few loose ends I couldn't
suss out for myself.
1. I'm not clear on any performance differences between
func (o *Type)
and
func (o Type)
Does the 2nd always make a copy of Type that ends up getting tossed?
What overhead does this entail?
2. Between an array and a slice, is there a performance penalty for
accessing an element in the slice?
3. Is there a way to pass function literals as arguments to other
functions? In my head I naively see syntax that looks something like
func receiver(func inner()) {
// setup
inner()
// tear down
}
func sender() {
// do stuff
receiver(func() {
// do more stuff
}())
// do final stuff
...if that makes any sense at all.
That's it offhand. Thanks for any info!
eric
A very neat tool!
> 1. I'm not clear on any performance differences between
>
> func (o *Type)
> and
> func (o Type)
>
> Does the 2nd always make a copy of Type that ends up getting tossed?
> What overhead does this entail?
It depends on the size of your struct. The bigger the struct, the
larger the memory copy required. The compiler guys would be able to
answer your question in more detail, but I would say in most cases
it's a negligible overhead.
> 2. Between an array and a slice, is there a performance penalty for
> accessing an element in the slice?
Again, I think any performance penalty would be negligible. I'd
suggest benchmarking against your particular application.
> 3. Is there a way to pass function literals as arguments to other
> functions? In my head I naively see syntax that looks something like
You most certainly can! It's one of the great features of Go.
Modifying your example to make it actually do something:
package main
import "fmt"
func receiver(inner func() string) {
fmt.Println("Hi " + inner())
}
func sender() {
receiver(func() string { return "exh" })
}
func main() { sender() }
> func receiver(func inner()) {
> // setup
> inner()
> // tear down
> }
>
> func sender() {
> // do stuff
> receiver(func() {
> // do more stuff
> }())
> // do final stuff
>
> ...if that makes any sense at all.
>
> That's it offhand. Thanks for any info!
> eric
>
Hope this helps.
Andrew
It's not remotely applicable at the moment, but I was thinking in
terms of audio processing. I believe the spec says that in traditional
arrays, the size is part of the type, and can only be set at compile
time, which makes them fairly unsuitable for storing buffers of audio
data (since the size would vary so drastically between users, and the
worst case would make them very large). So that leaves slices, but
any overhead at all on accessing elements, when you're doing that for
hundreds of streams running at 48 or 96 kHz, can be disastrous. Maybe
I'm missing something basic, or maybe Go just isn't intended for
content creation apps.
>> 3. Is there a way to pass function literals as arguments to other
>> functions? In my head I naively see syntax that looks something like
>
> You most certainly can! It's one of the great features of Go.
Sweet, thanks much, that's exactly what I wanted!
thanks,
eric
The only slice access overhead is the index bounds check,
and that applies to arrays too. Otherwise they behave just
like arrays in C. It seems like that should be plenty fast.
Russ