4

Change my mind. Golang can be more difficult than it needs to be sometimes:

Find the first "non-null" value in an array:

Go:
Optional<String> result = Stream.<Supplier<String>> of(
() -> first(),
() -> second(),
() -> third() )
.map( x -> x.get() )
.filter( s -> s != null)
.findFirst();

Ruby:
@group_list.find { |x| !x["list"].blank? }

Comments
  • 6
    Are you feeling proud about spreading misinformation, you scumbag?!

    This isn't Go, and you damn well know it.

    This looks like a raped version of java code.
  • 3
    At least credit stack overflow for copying their code, would you? https://stackoverflow.com/questions...
    Also that question is about Java, and I believe you never saw Go in your life, or it would be pretty obvious to you that the code you pasted is not even remotely close to Go syntax.
  • 2
    C# :
    @highlight

    array.FirstOrDefault(x => x != null);
  • 2
  • 3
    I haven't had coffee yet, and that's as much as it is Go, as it is PHP.

    Plus, there's no nulls in Go, there's nil's though 😬

    # fakeNews
  • 5
    Here are some examples:

    - Go variables are assigned using either var keyword followed by variable name and type (var a int = 1) or ":=" operator (a := 1), your code uses Java-style assignment (int a = 1)
    - "->" doesn't exist in Go, but there is "<-" which is a channel operator
    - there's no "null" in Go, it's called "nil"
    - function literals (lambda functions) in Go still require "func" keyword

    This is basic Go syntax, anyone who wrote 10 lines in Go knows these things. You're just a dipshit seeking attention and validation for the single fragile language you love by copying someone else's code without even knowing what language you're looking at.
  • 3
    @hitko you forgot that Go can't have a nil in a list of ints without some extra fuckery 😅

    But hard to argue your point.

    Mmmm coffee.

    @devphobe, did you mean Java and not Golang in your post? Or are you just trying to inflict pain on those of us that use many languages.
  • 1
    @C0D4 If you google "java find first not null" you get https://stackoverflow.com/questions... but if you google "golang find first not null" you get https://stackoverflow.com/questions...

    Someone just took the first google result...
  • 1
    Besides, in just about any language you can find the first non-null value in an array with a simple for(each) loop
  • 0
    Yes, Stack overflow for the credit. Shame on myself for not double checking my sources— you’re right, that example isn’t go .... but it’s still just as difficult to solve the challenge in go, is it not?

    Most other languages have list comprehension methods.
  • 1
    @devphobe you can't have a nil in a list in go, unless you created your own struct which supports a nil, but.... why would you bother going down that rabbit whole 🙃
  • 1
    @devphobe The Ruby example is about as readable as the Java one would be after compilation viewed as a bitmap image.
  • 0
    @C0D4 , I’m receiving JSON data that doesn’t always have the same shape, I need the first of :
    Email, Username, or Subscriber depending on what the API returned. Changing the API isn’t possible.
  • 0
    @C0D4 In many languages it’s a simple smash into a list and accept the first non null
  • 1
    @devphobe It's not just about the language. The question you copied the code from isn't even about finding the first non-null element in an array, it's about calling a list of functions until you get a non-null value, which is why it uses a stream of suppliers.

    Finding the first non-nil string in Go looks like this:

    for _, n := range a { if n != nil { return n } }

    But as @C0D4 pointed out that's not something you'd normally do in Go unless you somehow messed with the type system.

    Go doesn't have builtin utils like .map, .find, etc., and in fact, most compiled languages don't, because a simple for loop does the trick, and an explicit for loop makes it way easier for compiler to optimise such code.
  • 2
    Dude, even in *Java* (the lang that code clearly is) you can just do:

    Arrays.stream(arr).filter(x->x!=null).findFirst();

    ...or if you don't actually have an array:

    Stream.of(blah(), blah2(), blah3()).filter(x->x!=null).findFirst();

    ...and even that's verbose compared to most langs. Biggest prizes go to null safe langs where you don't need to do that at all.

    Is there a "write overly verbose code in one language and tag it as another language to piss everyone off" competition I'm missing?!
Add Comment