News for hackers. Stuff that matters.
Re:Go is not an easy language by harikb on Sunday February 21, 2021 @11:19PM (#26220807)
Going by their first example, the fact that I couldn't write

   list.delete(value)
without realizing it involves a linear search is considered one of the benefits of Go.

That said, I agree Go could use a bit for ergonomic and handy methods, while still remaining efficient.

The example on Concurrency also has a different answer. Go gives you all the tools, but there are many different ways their problem can be solved. By explicitly avoiding a "join" method and having any default 'channels', Go makes all these possible. What is missing is some of the popular patterns that emerged in the last few years need to make it to into stdlib or some cookbooks.

1. If you want a worker model, I would start n=3 go-routines and they all receive from a single channel. They don't need to much around with a channel of buffer 3, as in the example.

2. If the workers already return data from a compute, the read from driver serves as the wait.

3. In other cases, there is sync.Waitgroup available to synchronize completion status.

4. End of work from the driver can be indicated via a channel close. Closed channels can still be read from until they are empty and the reader can even detect end-of-channel.

Designing concurrent system is a bit complicated. Some tutorials do make it sound like a `go` keyword is all you need. All of these can fixed by improving std-lib or cookbooks.

Re:Go is not an easy language by arp242 on Monday February 22, 2021 @01:25AM (#26221458)
> Going by their first example, the fact that I couldn't write list.delete(value) without realizing it involves a linear search is considered one of the benefits of Go.

I think this is mostly okay. Most experienced programmers will realize this is a linear search and that it may be slow on large arrays. And turns out that in the overwhelming majority of the cases that's just fine!

For other cases it's not-so-fine, but the mere presence of "list.delete" doesn't really stand in the way of implementing another, more efficient, solution.

Overall, I certainly think it's better than implementing the same linear searches all the time yourself!

Re:Go is not an easy language by alkonaut on Monday February 22, 2021 @05:36AM (#26222929)
And it's not exactly gatekeeping to think that anyone who uses a method on any data structure on their platform should know the (rough) complexity of it (At least in the family constant, linear, or better/worse than linear). Removing a value from an array-backed list that isn't at the end is usually not a good idea.

My main problem with having "remove items by value" in standard library apis is that it assumes a notion of value equality. Having that notion inserted at the very "bottom" (Like Equals/GetHashCode in .NET for example) is a mistake that has caused an infinite amount of tears.

I much prefer this situation where the user must provide his own implementation and think about equality. It's boilerplate, but the boilerplate is useful here.

Re:Go is not an easy language by lstamour on Monday February 22, 2021 @01:15PM (#26228040)
> I much prefer this situation where the user must provide his own implementation and think about equality.

But that doesn’t scale beyond the first user. Every subsequent developer now needs to read implementations to understand what the code does thanks to a lack of standardization for these functions. Consider if there was a smaller standard library with fewer interfaces and conventions, it would become a lot harder to understand a number of concepts in Go, by design. That’s fine, but conventions are what made Ruby on Rails projects so successful that they scaled up to being the monolith monstrosities most of us with startup experience ended up knowing them to be.

Note that I’m suggesting something akin to C++’s standard library where algorithms are already written for you to use and compose with. Yes, the drawbacks are a slower compile time, and some conventions like constexpr can really complicate things, but… I can’t say that a larger standard library or a larger set of conventions would make Go harder to use assuming the implementations hide a sufficient amount of complexity to outweigh the overhead of learning of them in the first place.

What functions provide more value than the overhead required to learn them? Delete is one such function, mutable immutable data structures generally are likely another. Yes, the documentation needs to specify big O complexity, but it can still be easy to read. For example: https://immutable-js.github.io/immutable-js/docs/#/List

The only way to get easier than that would be to use the same syntax for immutable operations as mutable ones: https://immerjs.github.io/immer/docs/introduction

I recognize that the Go community finds the built-in standard library restrictive now, but that’s no reason not to support a versioned standard library that ships separately but contains common functionality. I can only point to TypeScript for how such a system might work, given the large community project that exists to provide and publish types for that language, without actually publishing them with the language or compiler itself, excluding browser dom etc.

Re:Go is not an easy language by alkonaut on Monday February 22, 2021 @01:33PM (#26228355)
> But that doesn’t scale beyond the first user. Every subsequent developer now needs to read implementations to understand what the code does thanks to a lack of standardization for these functions.

There is no standardization. It’s a hidden piece of logic that developers slowly and painfully understand.

If I you have to pass an equality function when removing an item from a list by value (or equivalently when creating a set or dictionary) it would always be explicit. That doesn’t mean it can’t be standardized. A framework can provide typical implementations (and often does!) such as “ReferenceEquals” or “StringComparison.Ordinal” etc.

Another unfortunate side effect of the “equality as a property of the type” is that you can only have one such equality. And if you are passed a Set of dogs you still can’t know for sure whether the equality used is actually the one declared in the Dog type of at Set creation (or possibly in the Animal base class). It’s a mess. And it’s so easy to avoid - the default virtual equality is simply not necessary.

Re:Go is not an easy language by lstamour on Monday February 22, 2021 @02:26PM (#26229237)
This is why I tend to like the example shown by the Immutable javascript libraries. They implement find-by-value using a library-specific calculation of equality built on primitives, but if you need something custom, you can pass predicate functions to, for example, perform your own equality check.

I think we're agreeing here, despite the initial disagreement. Standards don't have to solve every edge case, nor do they have to integrate with existing language patterns such as "equality as a property of the type" though a standard function could have multiple variants. The Go way of doing this might be like how regular expression function names are built.

Also, the library doesn't have to implement delete that way. If functional programming and predicate functions are an encouraged design pattern, you could replace delete-by-value functions with a filter function which could clearly indicate what happens if more than one value is found as well as how the equality check is performed but glosses over using slices to update the array. Some might say it's slower https://medium.com/@habibridho/here-is-why-no-one-write-gene... but it doesn't have to be slower, it's just tradeoffs in how the go compiler and language currently works vs could work.

Re:Go is not an easy language by simiones on Monday February 22, 2021 @02:40PM (#26229464)
Why would introducing value equality of all things be a problem? I think the opposite is true: many languages force you to write error-prone boilerplate because they lack a good definition of value equality built into the language and ecosystem.

Go in particular is worse on this front than any language more high-level than C. It defines equality for a very limited subset of built-in types, with no way to extend that notion of equality to anything that is not covered; nor any way to override the default equality assumptions. This makes it extremely painful whenever you want to do something even slightly advanced, such as creating a map with a struct as key when that struct has a pointer field .

And since pointers have lots of overloaded uses in Go, this turns a potentially small optimization (remember this field by pointer to avoid creating a copy) to a mammoth rewrite (we need to touch all code which was storing these as map keys).

Re:Go is not an easy language by kaba0 on Monday February 22, 2021 @02:16AM (#26221703)
I’m sorry for the “gatekeeping”, but do you really want to work together with someone that doesn’t know the language’s standard library or won’t even look at the documentation? And instead it would be positive that he/she writes the very implementation of something?
Re:Go is not an easy language by jholloway7 on Monday February 22, 2021 @02:54AM (#26221935)
This problem transcends documentation of any given language's standard library. list.delete(value) in any programming language is a degenerate without an ordering or hash built-in to the underlying data structure.

If you ever need to delete based on value, it's a smell that a list is the wrong data structure. I'm sure there are cases in constrained environments where a linear search is heuristically okay, but generally in application development list.delete(value) is a hint that you're using the wrong data structure.

Re:Go is not an easy language by mhaymo on Monday February 22, 2021 @05:46AM (#26222998)
Good point. I guess the reason the author finds removing from lists to be relevant is because there is no Set structure in the Go standard library. They should use a `map[t]bool`, but that is non-obvious to many programmers.
Re:Go is not an easy language by dullcrisp on Monday February 22, 2021 @08:41AM (#26224356)
My Google search suggests that maps in Go aren’t ordered. If so this doesn’t work in place of an array.
Re:Go is not an easy language by vlowther on Monday February 22, 2021 @09:44AM (#26225061)
`map[t]struct{}` saves you a few bytes per entry. Just use the `_,found := foo[key]` form of lookup
Re:Go is not an easy language by simiones on Monday February 22, 2021 @02:43PM (#26229503)
Unfortunately, map[t]bool (or map[t]struct{}) only works for special structs that happen to have equality defined for them. It's not a general purpose solution, it's just a hack.
Re:Go is not an easy language by nitrix on Monday February 22, 2021 @09:46AM (#26225089)
map[t]struct{} so that the values don't require any space.
Re:Go is not an easy language by citrin_ru on Monday February 22, 2021 @03:18AM (#26222068)
> Most experienced programmers will realize this is a linear search and that it may be slow on large arrays.

Experienced programmer can think that values in a list with such operation are indexed by a hash and delete by value is O(1) unless there is a big warning in documentation. Novice programmer probably haven’t seen such combination of an array and a hash table and will assume O(N) as taught in a college.

Re:Go is not an easy language by thezilch on Monday February 22, 2021 @04:02AM (#26222294)
Experienced programmers, if perf matters to their project, should think about their data structures, memory size, and access patterns. It's not clear why an experienced programmer would think an array defaults to wasting memory on a hash table. Why use an array, if they need lots of values and to delete them at random?
Re:Go is not an easy language by wizzwizz4 on Monday February 22, 2021 @03:23AM (#26222100)
Experienced programmers will know that such a thing is possible, but I've never seen any (non-JavaScript, non-PHP) language where primitive arrays involve a hash table.
Re:Go is not an easy language by Gaelan on Monday February 22, 2021 @03:42AM (#26222194)
And even in JS and PHP (and Lua) where arrays and hash tables are technically the same data structure, a hash table used as an array isn't actually indexed on elements, just numbers, so you don't get O(1) search.
Re:Go is not an easy language by shakow on Monday February 22, 2021 @06:18AM (#26223224)
Experienced programmer will know whether the vanilla array of the language they use feature this kind of convoluted data structure.

Which, for virtually all languages, is “obviously not”.

Re:Go is not an easy language by eweise on Sunday February 21, 2021 @11:48PM (#26220975)
Developers in other languages know the performance characteristics of list.delete because there is only one implementation throughout 99% of their codebase. Go you have to look at the implementation every single time to make sure its doing the right thing.
Re:Go is not an easy language by johnisgood on Monday February 22, 2021 @02:01AM (#26221625)
Haskell documentation uses the Big O notation everywhere which I really love. I wish Go and many other languages did the same.

An example would be https://hackage.haskell.org/package/containers-0.4.0.0/docs/....

  size :: Map k a -> IntSource
  O(1). The number of elements in the map.

  member :: Ord k => k -> Map k a -> BoolSource
  O(log n). Is the key a member of the map? See also notMember.

  lookup :: Ord k => k -> Map k a -> Maybe aSource
  O(log n). Lookup the value at a key in the map.
And so forth...
Re:Go is not an easy language by arp242 on Monday February 22, 2021 @02:34AM (#26221806)
Redis does the same; e.g. [1]:

> Time complexity: O(N) where N is the number of elements to traverse to get to the element at index. This makes asking for the first or the last element of the list O(1).

I agree this is something more documentations should do when possible; it doesn't even have to be big-O notation as far as I'm concerned, just a "this will iterate over all keys" will be fine.

Ruby's delete() doesn't mention any of this, although you can easily see the (C) implementation in the documentation[2]. In principle at least, this doesn't have to be O(n) if the underlying implementation would be a hash for example, which of course has its own downsides but something like PHP may actually do this with their arrays as they're kind of a mixed data structure? Not sure.

[1]: from https://redis.io/commands/lindex

[2]: https://ruby-doc.org/core-3.0.0/Array.html#method-i-delete

Re:Go is not an easy language by Groxx on Monday February 22, 2021 @02:37AM (#26221824)
Redis's documentation is wonderful, I wish every system I use were documented even half as well.
Re:Go is not an easy language by johannes1234321 on Monday February 22, 2021 @08:05AM (#26224037)
In the C++ standard the complexity of algorithms is a requirement for a compliant implementation. Many text books skip it, but better documentation references it. Taking the "delete element from list" example from this thread https://en.cppreference.com/w/cpp/container/list/remove states that there is a linear search.
Re:Go is not an easy language by mhh__ on Monday February 22, 2021 @03:12AM (#26222039)
Andrei Alexandrescu has a scheme to encode these in the D typesystem on his website. It didn't get merged into the standard library in the end but you can happily do it in your own code
Re:Go is not an easy language by btilly on Monday February 22, 2021 @01:21AM (#26221434)
My experience is that developers in other languages have no idea of the performance characteristics of list.delete. It seems low-level and therefore obviously fast.
Re:Go is not an easy language by mdemare on Monday February 22, 2021 @03:45AM (#26222219)
"Fancy algorithms are slow when n is small, and n is usually small." Rob Pike, creator of Go.
Re:Go is not an easy language by saagarjha on Monday February 22, 2021 @05:23AM (#26222836)
Not at the scale of his employer.
Re:Go is not an easy language by hiptobecubic on Monday February 22, 2021 @10:00PM (#26233402)
You don't need to be Google scale. Google scale is just "medium large startup" scale replicated across a zillion regions. Gmail has an enormous amount of data behind it, but it's not like calling foo.sort() is going to be iterating over billions of accounts.
Re:Go is not an easy language by andi999 on Monday February 22, 2021 @01:26AM (#26221462)
But isn't usually when using lists performance is dead anyway (due to cache misses).
Re:Go is not an easy language by simiones on Monday February 22, 2021 @02:48PM (#26229579)
"List" is usually a generic term for any data structure that can hold many items, and that has an API for adding or removing items. This can be implemented using an array, a linked-list, a hasthable, or a more advanced data structure.

Also, in languages with GCs, linked lists do not necessarily cause more cache misses than arrays, due to the way allocation works (this is especially true if you have a compacting GC, but bump-pointer allocation often makes it work even when you don't).

Re:Go is not an easy language by pansa2 on Monday February 22, 2021 @01:39AM (#26221527)
Linked-lists, maybe, but `list`s in Python are just resizable arrays.
Re:Go is not an easy language by kindall on Monday February 22, 2021 @02:21AM (#26221737)
resizable arrays of pointers to objects stored elsewhere, which is going to blow through your cache anyway, especially when you start accessing those objects' attributes. so it's more a memory management strategy than a performance optimization.
Re:Go is not an easy language by pansa2 on Monday February 22, 2021 @02:56AM (#26221945)
But when removing an item it’s not necessary to visit every element, just re-arrange the pointers (and in CPython, modify the reference count of the item that was removed).
Re:Go is not an easy language by sokoloff on Monday February 22, 2021 @07:44AM (#26223838)
When removing an item by value (being the use case in this thread), you do need to visit the elements to check the value.
Re:Go is not an easy language by hiptobecubic on Monday February 22, 2021 @10:02PM (#26233420)
Yes but the problem there is not that your list is backed by a linked list or an array, it's that every single value is boxed and needs dereferencing. The difference between no hops and one hop totally trumps one hop vs two hops.
Re:Go is not an easy language by jholloway7 on Monday February 22, 2021 @03:17AM (#26222061)
You can write a list data structure with performant 'delete' properties if you're willing to maintain a sort or a hash table. There would be people here bitching about memory usage if the stdlib did that natively. Here's a solution: don't use list.delete. You're using the wrong data structure if that's your solution to whatever collection you're maintaining.
Re:Go is not an easy language by jholloway7 on Monday February 22, 2021 @03:34AM (#26222153)
Downvotes on comp sci 102. Nice.
Re:Go is not an easy language by mirekrusin on Monday February 22, 2021 @04:47AM (#26222566)
We have this saying in my country "if grandma had a mustache she'd be a grandpa".
Re:Go is not an easy language by omaranto on Monday February 22, 2021 @09:08AM (#26224665)
I like my country's version better: "if grandma had wheels she'd be a bicycle".
Re:Go is not an easy language by arp242 on Tuesday February 23, 2021 @03:07AM (#26234893)
Which language/country is this?

I will use this in English regardless. I like it.

Re:Go is not an easy language by omaranto on Tuesday February 23, 2021 @10:07AM (#26238102)
Mexican Spanish. I don't know if they also use that saying in other Spanish speaking countries besides Mexico. In Spanish it goes: "Si mi abuelita tuviera ruedas, sería bicicleta".
Re:Go is not an easy language by jholloway7 on Monday February 22, 2021 @10:34AM (#26225637)
If your aunt had nuts she'd be your uncle
Re:Go is not an easy language by omaranto on Monday February 22, 2021 @11:31AM (#26226409)
That has the same basic idea as the mustache version, but is pithier. I still prefer the surrealism of the bicycle.
Re:Go is not an easy language by code_duck on Monday February 22, 2021 @05:16AM (#26222794)
You'd probably do better on HN to substitute 'decrying' for 'bitching'.
Re:Go is not an easy language by jholloway7 on Monday February 22, 2021 @10:56AM (#26225931)
Noted, thanks. Didn't realize that was the issue.
Re:Go is not an easy language by nunez on Tuesday February 23, 2021 @10:46AM (#26238595)
Yes!

Perfect example: Datetimes. In Golang, if you want to convert a string to a datetime (or vice versa), you _need_ to look at the godoc for datetime because it uses very specific permutations of "Jan 2, 2006" that you _have_ to include in your code. This is much more confusing than how this would be done in Python (provide the format of the date using ISO8601 notation) or Ruby (provide the string, get a Datetime, done).

Re:Go is not an easy language by moonchild on Monday February 22, 2021 @01:43AM (#26221545)
> without realizing it involves a linear search is considered one of the benefits of Go.

The primary problem that I think the author was trying to get at is that go lacks tools for building abstractions. In a language with generic functions (for instance), it would be possible to implement a delete_at function completely in user code. The fact that you cannot reflects poorly on the language.

Of course it will always be possible to write functions that have arbitrarily slow time complexities. That's something you have to be aware of, with any function; it's not a reason to avoid including a linear-time function to remove an element at a given index of an array.

Re:Go is not an easy language by grey-area on Monday February 22, 2021 @01:51AM (#26221582)
That’s a deliberate choice in many ways, and it means you don’t end up building castles in the air, or worse, working within someone else’s castle in the air. See the lack of inheritance for a good example of a missing abstraction which improves the language.

Abstractions are pretty and distracting but they are not why we write code and each one has a cost, which is often not apparent when introduced.

That’s not to say Go is perfect, there are a few little abstractions it would IMO be improved by having and the slice handling in particular could be more elegant and based on extending slice types or a slices package rather than built in generic functions.

Re:Go is not an easy language by kaba0 on Monday February 22, 2021 @02:13AM (#26221685)
I don’t see why a well-built standard library would be in the way. In the case of delete, well write your own linear search if you are into, though it gets old imo fast. But will you also write a sort algorithm by hand? Because I am fairly sure it won’t be competitive with a properly written one. Also, what about function composition? Will you write a complicated for loop with ifs and whatnot when a filter, foldl and friends would be much more readable?
Re:Go is not an easy language by cube2222 on Monday February 22, 2021 @02:26AM (#26221760)
Go actually does contain a sort package in the stdlib: https://golang.org/pkg/sort/ with the most ergonomic function being sort.Slice

As for a complicated for loop instead of the functional equivalent: I too am a fan of functional patterns in this case, but the for loop equivalent in my opinion is just as readable in 99% of cases. A little more verbose, that's all. Matter of taste.

Re:Go is not an easy language by Gibbon1 on Monday February 22, 2021 @03:59AM (#26222286)
Seems to me there are a lot of different types of collections which all have a delete operation. Feels like go forcing you to hand roll a delete forces you to violate the principal that a bit of code should care as little as possible about things it's not about.

If code breaks because list is now a hashmap, that seems like an anti-feature.

Re:Go is not an easy language by hmage on Monday February 22, 2021 @09:28AM (#26224910)
> the principal that a bit of code should care as little as possible about things it's not about

Can you find/quote where that principle comes from? I'm genuinely curious.

Re:Go is not an easy language by kaba0 on Monday February 22, 2021 @03:47PM (#26230365)
I think it is mostly based on OOP paradigm, like principle of least knowledge.
Re:Go is not an easy language by Gibbon1 on Monday February 22, 2021 @05:45PM (#26231690)
It's a motivation for things like OOP, Generics, and macro's.
Re:Go is not an easy language by grey-area on Monday February 22, 2021 @02:32AM (#26221798)
I agree filtering and many generic operations on slices would be nice, I think they have plans for it once generics lands. I don’t agree they are huge problems though, they are much simpler than sorting algorithms for example (which are included).

A range vs a series of filtering operations is an interesting question in terms of which is easiest to write or, crucially, for a beginner to understand after writing - not convinced foldl is easy for beginners. If you like abstractions like that, Go will be abhorrent to you, and that’s ok.

It is easy to build your own filter on a specific type, I’ve only done it a couple of times, it is literally no more than a for loop/range. Where I have written extensions and would appreciate helpers in the std lib is things like lists of ints and strings - contains, filter and friends would be welcome there in the stdlib (I use my own at present).

For filter etc I think I’d use them but am not convinced they would change the code much - the complexity is in what you do to filter not the filtering operation itself which probably adds about 3 lines and is very simple (set up new list, add to list in range, return list). For example in existing code I have lots of operations on lists of items which sit behind a function and for hardly any of them would I delete the function and just use filter in place at the call site, because I have the function to hide the complexity of the filtering itself (all those if conditions), which would not go away. I wouldn’t even bother rewriting the range to use filter because it’s not much clearer.

Re:Go is not an easy language by stiray on Monday February 22, 2021 @02:13AM (#26221682)
> and it means you don’t end up building castles in the air, or worse, working within someone else’s castle in the air. See the lack of inheritance for a good example of a missing abstraction which improves the language.

Very well said. If only more developers would understand this.

Yes I know, creating interfaces, inheriting everything from the void, adding as large amount of layers to create the beautifully constructed reusable structure is a nice drive but it might happen no one will reuse it as doesn't want to dive itself trough infinite level lasagna with rolling ravioli around it (and - thanks god, go doesn't have try/catch - throwing exceptions trough all the layers to force you to go down the guts of the construct to figure out what it means). Or, as it often happens, will be forced to reuse it, complicating his life and is code.

I do understand that inheritance, even operator overloading has its meaning and is extremely useful. But it has another side - everybody are overdoing it as "might come handy later" and the code becomes a case for "How to write unmaintainable code"[2].

When I am coding I am not doing it for philosophical reasons and go has until now succeeding being a very helpful tool without much of "meaning of life" complications. And i would love to see it stay that way.

If you are forcing me to read the documentation / code (presumably I know what I am trying to solve) to be able to use the "beautiful oo construct" and forcing you to read your beautiful design, you have failed making it and I have seen it in java everywhere. I just hope the same people making everything complicated more than it need to be wont skip from java[1] train to go train. I really don't want them anywhere close.

[1]https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...

[2]https://github.com/Droogans/unmaintainable-code (and 20 others)

Re:Go is not an easy language by marcus_holmes on Monday February 22, 2021 @04:42AM (#26222532)
Yeah, this. I always found myself creating beautiful OO constructs that were invariably overfitted to my understanding of the state of the problem at that time, and were really hard work to change once I understood the problem better (or it changed).
Re:Go is not an easy language by doctor_eval on Monday February 22, 2021 @05:52AM (#26223047)
Yeah I’ve fallen into this trap and worst of all, I’ve had my team fall into it and not find their way out.

Abstractions are a great idea, and Go has plenty of these (io.Writer is ubiquitous), but most business logic is dead boring and doesn’t need it.

Re:Go is not an easy language by d3nj4l on Monday February 22, 2021 @12:20AM (#26221145)
The advantage here isn’t that it’s ergonomic, it’s that if I get an “unknown” array I have no special knowledge about, I can simply use list.delete instead of having to write a linear delete and sleep soundly in the knowledge that it’ll have well-known performance characteristics for that case. If I know something special about an array, I’m still free to write my own deletion algorithm, and nobody would fault you for that.
Re:Go is not an easy language by dmitriid on Monday February 22, 2021 @03:08AM (#26222010)
> What is missing is some of the popular patterns that emerged in the last few years

Erlang has had these "popular patterns" for decades. And all languages that ignore them, making devs reimplement them, poorly, with third-party libs and ad-hoc solutions.

Re:Go is not an easy language by jnwatson on Monday February 22, 2021 @03:22AM (#26222098)
It has been said that Go4 software design patterns are signs of language defects.

Most all of them can be implemented as single lines of code in Python.

Re:Go is not an easy language by koenigdavidmj on Monday February 22, 2021 @09:16AM (#26224778)
Most languages codify common idioms from earlier ones; I don’t see how most of the patterns are any different.

I’m sure there were a lot of assembly programs that used the “call stack pattern”. I’m sure there are a lot of C programs that abuse struct packing to do inheritance (the “class pattern”, sometimes with crappy vtables) or the preprocessor to do “templates”. And even in Python, you’ve got to use the visitor pattern due to single dispatch.

Re:Go is not an easy language by technics256 on Sunday February 21, 2021 @11:38PM (#26220920)
I'm just learning Go myself, where can I learn or reference these popular patterns that have emerged over the past few years?
Re:Go is not an easy language by harikb on Sunday February 21, 2021 @11:42PM (#26220939)
What comes handy is this [1] link, but I will update if I get any better links or others might chime in.

[1] https://blog.golang.org/pipelines

It is a very long doc, but that also shows that concurrency has so many patterns one might like.

My own pattern is typically

1. Decide level of parallelism ahead of time and start workers (that many `go X()` invocations 2. Setup sync.Waitgroup for the same count 3. Create two channels, one for each direction. 4. Job itself needs some sort of struct to hold details

Most of my jobs don't support mid-work cancelation, so I don't bother with anything else.

Re:Go is not an easy language by rakoo on Monday February 22, 2021 @02:26AM (#26221758)
Your 1. and 2. are merged in the almost-stdlib package errgroup: https://pkg.go.dev/golang.org/x/sync/errgroup.

It also uses context (useful for long running, concurrent jobs) and handles mid-work cancellation

Re:Go is not an easy language by majewsky on Monday February 22, 2021 @07:45AM (#26223851)
Study the generic reader/writer implementations in the io module. (On my system, those sources are in /usr/lib/go/src/io.) The io.Reader and io.Writer interfaces are very simple, but very powerful because of how they allow composition. A shell pipeline like `cat somefile.dat | base64 -d | gzip -d | jq .` can be quite directly translated into chained io.Readers and io.Writers.

Another example of this is how HTTP middlewares chain together, see for example all the middlewares in https://github.com/gorilla/handlers. All of these exhibit one particular quality of idiomatic Go code: a preference for composition over inheritance.

Another quality of idiomatic Go code is that concurrent algorithms prefer channels over locking mechanisms (unless the performance penalty of using channels is too severe). I don't have immediate examples coming to mind on this one though, since the use of channels and mutexes tends to be quite intertwined with the algorithm in question.

Re:Go is not an easy language by wisswazz on Monday February 22, 2021 @12:52AM (#26221310)
Re:Go is not an easy language by majewsky on Monday February 22, 2021 @07:37AM (#26223783)
This looks like some C++ or Java developer trying to shoehorn OOP concepts into a language that is not OOP-first. Just to pick out one particularly egregious example:

> Registry: Keep track of all subclasses of a given class

Go does not have classes.

Re:Go is not an easy language by joelfolksy on Monday February 22, 2021 @11:40AM (#26226531)
Since simpler languages are easier to work with, let's just remove goroutines and channels from Go. Then all these problems will just go away.
Re:Go is not an easy language by SergeAx on Tuesday February 23, 2021 @11:54AM (#26239576)
It's okay. They will add generics soon, someone will immideately write a generic array.Delete() function, it will klog compiling and linking, and in couple more iterations we will have another JVM.
Re:Go is not an easy language by chinmaythosar on Sunday February 21, 2021 @11:30PM (#26220880)
Linear search if it's not sorted. If it's sorted binary search will be much faster. So you need a flag for the function to tell it to use one of the two. This is why writing generic functions can be tricky. Programmers may use something inefficient just because it makes their lives easier.
Re:Go is not an easy language by z0r on Sunday February 21, 2021 @11:53PM (#26220999)
linear search can be faster than binary search for small arrays, depending on your processor architecture.

writing generic functions is difficult, so it's nice if a language allows people to do so, otherwise you get N inefficient and/or buggy reimplementations of the function in every project needing it. (not sure if that is your point)

Re:Go is not an easy language by arp242 on Monday February 22, 2021 @02:36AM (#26221815)
Linear search is also quite a bit faster than maps for small arrays (in Go at least, last time I tested it).
Re:Go is not an easy language by hderms on Monday February 22, 2021 @11:52AM (#26226691)
Re:Go is not an easy language by chinmaythosar on Monday February 22, 2021 @12:29AM (#26221199)
my point was that the author uses an example like search and called go not an easy language. so i was trying to give an example where it's not necessary a linear search or search is trivial ...
Re:Go is not an easy language by anonymoushn on Monday February 22, 2021 @01:19AM (#26221425)
If your developers hand write a binary search to delete an element you are extremely likely to end up with bugs. So it would be nice if there was a generic binary search too.
Re:Go is not an easy language by BenFrantzDale on Monday February 22, 2021 @06:51AM (#26223458)
That Go doesn’t have the equivalent of `std::lower_bound` is pretty ridiculous.
Re:Go is not an easy language by jtsiskin on Monday February 22, 2021 @02:47AM (#26221891)
Binary search won’t be any faster (in big O) because it’s backed by an array: you’ll still need to copy every element after the removed one from slot n to n-1.
Re:Go is not an easy language by harikb on Sunday February 21, 2021 @11:37PM (#26220918)
Does Ruby track that a list is already sorted? If not, you are still adding cognitive overhead for the programmer to track it
Re:Go is not an easy language by vinger on Monday February 22, 2021 @01:19AM (#26221424)
You can sort by many things. Knowing where you sort (usually as far up the chain as possible) is important
Re:Go is not an easy language by prepperdev on Sunday February 21, 2021 @11:22PM (#26220825)
Yes. That very example shows why itis hard to write fast program in Ruby. It's not even the interpreter (which is slow), but it's that Ruby is pure magic.

I was never able to understand what happens, when I looked at a particular snippet of Ruby code, if it was not me who wrote it. With Go, understanding others people code is a trivial task.

Re:Go is not an easy language by d3nj4l on Monday February 22, 2021 @12:16AM (#26221123)
What part of

    list.delete(x)
Is hard to understand if you didn’t write it?
Re:Go is not an easy language by theshrike79 on Tuesday February 23, 2021 @03:29AM (#26235031)
If we're still talking about Ruby here: List could be _anything_. It might be a list of numbers, it can be a dataset in a remote server, it can be a web page parsed into a list of sentences, it might be list of people in Active Directory.

You literally can't know what's going on under the hood without popping it and looking for yourself.

Ruby's infatuation with clever magic code is what turned me off it years ago. You can get up to 80% in 20% of the time compared to other languages, but then you spend the 80% of time you have left fighting against the magic to get the last 20% done properly. There's way too much stuff You Just Need To Know.

Re:Go is not an easy language by philosopher1234 on Monday February 22, 2021 @02:00AM (#26221619)
I don’t think that example tells the whole story. You have to zoom out and consider what code based look like when this trade off is made repeatedly by devs. When “easyness” or DRYness becomes king you get ravioli code, and it becomes unintelligible. Keep it simple
Re:Go is not an easy language by majewsky on Monday February 22, 2021 @07:47AM (#26223873)
My persistent impression of the Ruby ecosystem is that everyone optimizes for clever one-liners so hard that all other priorities go out the window.
Re:Go is not an easy language by nobleach on Sunday February 21, 2021 @11:36PM (#26220905)
As much as I don't personally enjoy writing Go. This is a huge benefit. There's a decent amount of conformity around the "right way" of doing things. I'll admit, it has definitely made me consider my approach to solving problems in other languages much simpler. I'm not back to writing loops in JavaScript when I use it. It really bugs the functional programming folks but... no one can argue that it's readable, and it's pretty fast.