News for hackers. Stuff that matters.
Re:Down the Golang Nil Rabbit Hole by j-james on Tuesday March 30, 2021 @12:34AM (#26631286)
Lack of generics and clumsy error handling would be my guess.
Re:Down the Golang Nil Rabbit Hole by BobbyJo on Tuesday March 30, 2021 @12:47AM (#26631346)
In practice, those have honestly been the two largest assets to me. No generics means not worrying about a coworker going off the rails and creating a library where it's not needed (me being that coworker often in my past), and clumsy error handling means factoring said error handling into your APIs, making you much more deliberate about where and how errors are eaten. Having used Java extensively, those two 'warts' have honestly saved me a lot of heartache.
Re:Down the Golang Nil Rabbit Hole by somezero on Tuesday March 30, 2021 @02:22AM (#26631686)
How much do your colleagues (ab)use goto statements that Go provides? :)
Re:Down the Golang Nil Rabbit Hole by tirrex on Tuesday March 30, 2021 @03:04AM (#26631889)
C++ has goto, Java has “break <label>“, nobody uses it but you see a lot of generics abuse.
Re:Down the Golang Nil Rabbit Hole by calessian on Tuesday March 30, 2021 @06:45PM (#26641475)
I’ve seen break label a handful of times for nested loops. Whether that was the best choice is of course always up for debate. It’s definitively possible to replace and not a necessary language feature.

Maybe it’s a good thing it’s not widely known, generics are trivial to discover.

Re:Down the Golang Nil Rabbit Hole by saturn_vk on Tuesday March 30, 2021 @06:37AM (#26633017)
What can be classified as generics abuse in Java?
Re:Down the Golang Nil Rabbit Hole by BobbyJo on Tuesday March 30, 2021 @11:53AM (#26636122)
Using generics anywhere only a single type is ever used. The ambiguity really just hurts readability and creates a landmine for future engineers that think other types can be used willy-nilly and things will behave as they should. Especially if some optimizations get done underneath that assume or asserts some set of types, but doesn't enforce them at the API level.
Re:Down the Golang Nil Rabbit Hole by masklinn on Tuesday March 30, 2021 @09:05AM (#26634110)
Using java is abusive towards generics, they didn't deserve that?
Re:Down the Golang Nil Rabbit Hole by BobbyJo on Tuesday March 30, 2021 @11:35AM (#26635833)
I've literally never seen them. Not even once.
Re:Down the Golang Nil Rabbit Hole by TheDong on Tuesday March 30, 2021 @01:22AM (#26631466)
One of them has to be the for loop reusing value thing.

    func processItem(item *string) {
      if item == nil {
        fmt.Println("nil item")
        return
      }
      fmt.Println(*item)
    }
    
    stuffToProcess := []string{"one", "two"}
    wg := sync.WaitGroup{}
    for _, item := range stuffToProcess {
      wg.Add(1)
      go func(s *string) {
        time.Sleep(1 * time.Millisecond)
        processItem(s)
        wg.Done()
      }(&item)
    }
    wg.Wait()
    // What do I print?
Playground here: https://play.golang.org/p/mouvT1BkpNJ
Re:Down the Golang Nil Rabbit Hole by zimpenfish on Tuesday March 30, 2021 @03:17AM (#26631960)
gosec[1] will warn you about this, at least.

    [reuse.go:26] - G601 (CWE-118): Implicit memory aliasing in for loop. (Confidence: MEDIUM, Severity: MEDIUM)
    25:    wg.Done()
  > 26:   }(&item)
    27:  }
[1] https://github.com/securego/gosec
Re:Down the Golang Nil Rabbit Hole by JulianMorrison on Tuesday March 30, 2021 @06:33PM (#26641399)
Easily enough fixed by item := item (that is, shadow the variable with something new that won't be overwritten)
Re:Down the Golang Nil Rabbit Hole by mimischi on Tuesday March 30, 2021 @02:41AM (#26631766)
Can‘t this be fixed by changing bits to be passed by value?

https://play.golang.org/p/srAyKZN2NTH

Re:Down the Golang Nil Rabbit Hole by thwarted on Tuesday March 30, 2021 @02:55AM (#26631836)
Yes, it is odd to pass the address of a local variable from the enclosing scope like this to a go routine.
Re:Down the Golang Nil Rabbit Hole by masklinn on Tuesday March 30, 2021 @03:52AM (#26632139)
Well that's true in the sense that usually you'd just close over it, which is the classic broken case of closing over a mutable binding: https://play.golang.org/p/40FRmvhifBl

I assume GGP's contrived example is to show the underlying mechanics of the issue.

Re:Down the Golang Nil Rabbit Hole by latch on Tuesday March 30, 2021 @12:57AM (#26631389)
I think these two rightfully get highlighted, but I've found it difficult to organize complex Go code (though clearly some people manage), which I think is now my biggest issue with the language.

It's inability to deal with cyclic references, the lack of overloading, the lack of package-level visibility and the lack of static functions all contribute to a lot of ceremony.

You end up with functions like newUserWithPassword and NewRole (casing intentional), instead of User::new() and Role::new()

You could just have a NewUser that takes a NewUserOpts which exposes a fluent interface. But again, a lot of ceremony.

You can use a package per type, but still no overloading, and you'll need a 3rd package to bridge the two if the reference each other.

Re:Down the Golang Nil Rabbit Hole by kodah on Tuesday March 30, 2021 @01:02AM (#26631406)
> It's inability to deal with cyclic references, the lack of overloading, the lack of package-level visibility and the lack of static functions all contribute to a lot of ceremony.

After 4 years of Go, this is usually a design thing. Once I figured out how to separate and isolate domains of interest better, I stopped running into these issues entirely. My code, in and outside of Go, is much better for it. I also learned that much of "thread-safe" programming is taught this way, which makes sense.

Re:Down the Golang Nil Rabbit Hole by christophilus on Tuesday March 30, 2021 @06:45AM (#26633070)
F# has similar constraints, and gets similar complaints, and yet advocates (like I) think it’s a feature, not a bug. Most problems can be decomplected, and I like that Go enforces this particular constraint.
Re:Down the Golang Nil Rabbit Hole by wizhi on Tuesday March 30, 2021 @04:30AM (#26632304)
Do you possibly have any example projects you would be willing to share?

I've been getting into Go for the past couple of months, but I still struggle with this.