Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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. -
hitko31454yAt 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. -
C0D4669014yI 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 -
hitko31454yHere 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. -
C0D4669014y@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. -
hitko31454y@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... -
hitko31454yBesides, in just about any language you can find the first non-null value in an array with a simple for(each) loop
-
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. -
C0D4669014y@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 🙃
-
@devphobe The Ruby example is about as readable as the Java one would be after compilation viewed as a bitmap image.
-
@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. -
@C0D4 In many languages it’s a simple smash into a list and accept the first non null
-
hitko31454y@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. -
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?!
Related Rants
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? }
rant
golang
ruby