@nodir, sure, but then you have to invent a new, special purpose syntax that is only used when the func
declaration is elided. Do you see the negative implications for comprehension and readability this would bring ?
Yea. I just wish there was a more collaborative place. We could find out pitfalls and upsides. Godoc.org IMHO should focus on indexing documentation only. I donāt want to see a bunch of +1s and reviews every time I go to read the docs
Personally, I like brevity, but it is another concept that would need to be explained to beginners. I guess Iād wonder:
- When should arrow syntax be encouraged / dis?
- How could we avoid the need for team disputes and style guides? Iām thinking in terms of our effective Go guide.
- Would the scope* remain the same for an arrow function?
- Does this encourage inline implementations of functions; instead of isolating them and referencing them in a modular fashion? Idk.
To be fair, I do think that this has become relatively standard, and most developers are able to read this.
* this is the inconsistency I see with most languages; does it include surrounding scope or only mimic a new function scope
Yes, different func literal syntax adds complexity indeed. Maybe we can make it look more like the existing syntax.
First, reuse func
but omit types:
var f func(x int)
f = func(x) { fmt.Println(x) }
Second, if func(...)
is followed up by = and an expression, treat it as a return expression
f := func(x int) int = x+1
// same as
f := func(x int) int { return x+1 }
This gives us
var f func(func(int) int)
f(func(x) = x+1)
// same as
f(func(x int) int { return x+1 })
AFAIK, C# and presumably Java does not have this inconsistency. Arrow function is just a shorter syntax.
Very true. As long as we do not include the surrounding scope, Iām fine. I think it is becoming pretty standard.
var list []HugeStruct
for i, e := range list {
//...
}
does O(size(HugeStruct) * len(list)) of memory copying. One would have to write
for i := range list {
e := &list[i]
//...
}
to avoid that.
Would be great if we didnāt have to do to that:
for i, *e := range list {
// e === &list[i]
}
WDYT?
actually, []HugeStruct
is a bad idea anyway because append
would reallocate huge array each time an item doesnāt fit. Should use []*HugeStruct
, so nvm my previous message.
I carried this idea for a while and eventually dropped it, regular if
statements are totally fine. I wouldnāt even call it a āwishā, itās just a random āwhat if itās been hereā.
You basically miss a by-reference foreach from C++, which is imo a great thing and a common pitfall in Go (people tend to expect e
to be sort of a const reference). Itās been discussed a lot already. IIRC, guys from Go team (=Pike & Co.) found it weird and decided that this thing should not be added to the language. I donāt agree with that though.
Also, correct me if I am wrong, but Iāve got a funky feeling that go compiler has optimizations against excessive copying in for-range.
āCā flavor static variables ie variables that are declared inside a function but persist across function calls. I end up making a bunch of global variables that just clutter the program.
If you want an equivalent to static variables youāre looking for closures. But if this is a systemic problem for you Iād argue that you probably need to rethink how your code is organized.
closures constitute a complicated solution to a simple problem. They still end up being global scope and thus create the same amount of clutter as a simple variable. Sometimes the problem is as simple as counting the number of times a function is called, or resetting some local state after the function has been called ānā times. A pretty-printer that counts lines is a simple example. You want to reset the linecount every 55 lines for example. Sure there are other ways to do it, but a static variable solved it pretty well in C. And I miss that simplicity.
Static variables are just global variables with hidden names. That means they have many of the same problems as globals, such as two different places unintentionally using the same state, or even worse: concurrent modification resulting in data races. Closures can give you the same benefits without those problems.
A counter for example: https://play.golang.org/p/nOkda9OW7P
A line pretty printer for example: https://play.golang.org/p/0SF0iIIsEW
This solves the same thing as static variables with only a couple extra lines of code, while avoiding all the pitfalls. And if you really, really, want global static variables, you can do that too: https://play.golang.org/p/kNY2iTNynf
after i reading this topic i understood that __attribute__ (( packed))
are missing. basically is not big deal but if someone want to write a kernel can be a big problem.
From everything Iāve read, Go really isnāt a kernel language anyway.
Your email client may not be compatible with Discourse.
Your email client may not be compatible with Discourse.
Thanks Dave, that would be sad/amusing. Iām using GMail āreply.ā I suspect itās because I use a custom email address for this forum, and Discourse doesnāt know my replies are āme.ā
Anyway my original reply was supposed to read:
You might want to poke around a bit and keep reading then, Iāve read a number of discussions describing how to use Go for kernel development. Itās a lively subject and I donāt think thereās any consensus that Go is inappropriate for kernel development.
To add to that, Iād refer to this thread on golang-nuts:
https://groups.google.com/forum/#!topic/golang-nuts/wEUiS4UyiKs