I think in Golang, Array, Slice, and Map , all of them are very difficult to use。

3,423 views
Skip to first unread message

小菜

unread,
Jun 20, 2014, 4:47:38 AM6/20/14
to golan...@googlegroups.com

I  think in Golang, Array, Slice, and Map , all of them are very difficult to use。

For example, I want to check whether an element in the array, get all the map key, or the map value into slice,

With two slice,  intersection or union, the Slice sort, map sort, I found that the operation has no official function support, there is no official  package, so it is very difficult to use, not a standard, efficient way.

Perhaps Golang should like PHP/Python built-in some common operations,  Programming language is used to solve practical problems。


Reference resources.

Http://us1.php.net/manual/en/ref.array.php


  • array_change_key_case — Changes the case of all keys in an array
  • array_chunk — Split an array into chunks
  • array_column — Return the values from a single column in the input array
  • array_combine — Creates an array by using one array for keys and another for its values
  • array_count_values — Counts all the values of an array
  • array_diff_assoc — Computes the difference of arrays with additional index check
  • array_diff_key — Computes the difference of arrays using keys for comparison
  • array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_diff_ukey — Computes the difference of arrays using a callback function on the keys for comparison
  • array_diff — Computes the difference of arrays
  • array_fill_keys — Fill an array with values, specifying keys
  • array_fill — Fill an array with values
  • array_filter — Filters elements of an array using a callback function
  • array_flip — Exchanges all keys with their associated values in an array
  • array_intersect_assoc — Computes the intersection of arrays with additional index check
  • array_intersect_key — Computes the intersection of arrays using keys for comparison
  • array_intersect_uassoc — Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison
  • array_intersect — Computes the intersection of arrays
  • array_key_exists — Checks if the given key or index exists in the array
  • array_keys — Return all the keys or a subset of the keys of an array
  • array_map — Applies the callback to the elements of the given arrays
  • array_merge_recursive — Merge two or more arrays recursively
  • array_merge — Merge one or more arrays
  • array_multisort — Sort multiple or multi-dimensional arrays
  • array_pad — Pad array to the specified length with a value
  • array_pop — Pop the element off the end of array
  • array_product — Calculate the product of values in an array
  • array_push — Push one or more elements onto the end of array
  • array_rand — Pick one or more random entries out of an array
  • array_reduce — Iteratively reduce the array to a single value using a callback function
  • array_replace_recursive — Replaces elements from passed arrays into the first array recursively
  • array_replace — Replaces elements from passed arrays into the first array
  • array_reverse — Return an array with elements in reverse order
  • array_search — Searches the array for a given value and returns the corresponding key if successful
  • array_shift — Shift an element off the beginning of array
  • array_slice — Extract a slice of the array
  • array_splice — Remove a portion of the array and replace it with something else
  • array_sum — Calculate the sum of values in an array
  • array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_udiff — Computes the difference of arrays by using a callback function for data comparison
  • array_uintersect_assoc — Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc — Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions
  • array_uintersect — Computes the intersection of arrays, compares data by a callback function
  • array_unique — Removes duplicate values from an array
  • array_unshift — Prepend one or more elements to the beginning of an array
  • array_values — Return all the values of an array
  • array_walk_recursive — Apply a user function recursively to every member of an array
  • array_walk — Apply a user function to every member of an array
  • array — Create an array
  • arsort — Sort an array in reverse order and maintain index association
  • asort — Sort an array and maintain index association
  • compact — Create array containing variables and their values
  • count — Count all elements in an array, or something in an object
  • current — Return the current element in an array
  • each — Return the current key and value pair from an array and advance the array cursor
  • end — Set the internal pointer of an array to its last element
  • extract — Import variables into the current symbol table from an array
  • in_array — Checks if a value exists in an array
  • key_exists — Alias of array_key_exists
  • key — Fetch a key from an array
  • krsort — Sort an array by key in reverse order
  • ksort — Sort an array by key
  • list — Assign variables as if they were an array
  • natcasesort — Sort an array using a case insensitive "natural order" algorithm
  • natsort — Sort an array using a "natural order" algorithm
  • next — Advance the internal array pointer of an array
  • pos — Alias of current
  • prev — Rewind the internal array pointer
  • range — Create an array containing a range of elements
  • reset — Set the internal pointer of an array to its first element
  • rsort — Sort an array in reverse order
  • shuffle — Shuffle an array
  • sizeof — Alias of count
  • sort — Sort an array
  • uasort — Sort an array with a user-defined comparison function and maintain index association
  • uksort — Sort an array by keys using a user-defined comparison function
  • usort — Sort an array by values using a user-defined comparison function

egon

unread,
Jun 20, 2014, 5:15:24 AM6/20/14
to golan...@googlegroups.com


On Friday, 20 June 2014 11:47:38 UTC+3, 小菜 wrote:

I  think in Golang, Array, Slice, and Map , all of them are very difficult to use。

For example, I want to check whether an element in the array, get all the map key, or the map value into slice,

With two slice,  intersection or union, the Slice sort, map sort, I found that the operation has no official function support, there is no official  package, so it is very difficult to use, not a standard, efficient way.

 
You are making suggestions without having learned the language nor taken time to understand its design decisions. First learn the language, learn why Go team has decided to omit those things, and then try to argue for changes.

The simplest way I can explain is that there are many different ways to do "intersection", "union"... and they have different performance/memory characteristics... it depends on the context what is efficient.

The simplest way I can explain is that.. Go does not hide the performance characteristics of different operations. To check whether an element is an array takes O(N) which means it will look O(N) in the code. To get all the keys from a (go) map is also an O(N) algorithm, which will look like O(N) in the code.

+ egon

Benjamin Measures

unread,
Jun 20, 2014, 5:24:17 AM6/20/14
to golan...@googlegroups.com
On Friday, 20 June 2014 09:47:38 UTC+1, 小菜 wrote:
arsort — Sort an array in reverse order and maintain index association
asort — Sort an array and maintain index association
krsort — Sort an array by key in reverse order
ksort — Sort an array by key
natcasesort — Sort an array using a case insensitive "natural order" algorithm
natsort — Sort an array using a "natural order" algorithm
rsort — Sort an array in reverse order
sort — Sort an array
uasort — Sort an array with a user-defined comparison function and maintain index association
uksort — Sort an array by keys using a user-defined comparison function
usort — Sort an array by values using a user-defined comparison function

PHP is a fractal of bad design[1], so perhaps isn't the best example.

Instead of asking for things to be more like PHP, can you describe a concrete problem (with code) that you have? That way, somebody might be able to help you.

oju...@gmail.com

unread,
Jun 20, 2014, 6:10:18 AM6/20/14
to golan...@googlegroups.com
Our colleague 小菜 has some hard time writing in English. His text gets a severe and direct tone I am sure is not exactly the original intent, so we should take that into consideration when reading his messages.

He is trying to say that some common operations on containers could be easier and cites as an example the PHP library.

Well, both Go and PHP are programming languages. Other than that, they are entirely different beasts.

In general terms I tend to agree with 小菜, there is room for improvements, but everything in it's due time. If Go is going to have some of that functionality in future, it will have to fit nicely with everything else. Simply there is not the necessary infrastructure in the language for such additions right now. Maybe in future. Who knows?

Larry Clapp

unread,
Jun 20, 2014, 7:57:54 AM6/20/14
to golan...@googlegroups.com
I don't know PHP, but from what I've seen of it, it looks a lot like Perl.  I know Perl fairly well.

One of the key differences between Perl and Go (and possibly PHP and Go) is that Go has structs and Perl has only hashes (as far as named data structures)[1].  It's very important to not try to write Perl in Go (i.e. duplicating your Perl hashes in Go hashes, etc), but write more native, idiomatic Go, that takes full advantage of Go's structs and other native data structures.

I agree with Benjamin Measures, "can you describe a concrete problem (with code) that you have"?

You might also like to read How To Ask Questions The Smart Way, http://www.catb.org/esr/faqs/smart-questions.html.  See also http://www.catb.org/esr/faqs/smart-questions.html#translations.

-- Larry


[1] It's possible to get more exotic with closures and such, but in the core language, hashes are pretty much it.



On Friday, June 20, 2014 4:47:38 AM UTC-4, 小菜 wrote:

I  think in Golang, Array, Slice, and Map , all of them are very difficult to use。

For example, I want to check whether an element in the array, get all the map key, or the map value into slice,

With two slice,  intersection or union, the Slice sort, map sort, I found that the operation has no official function support, there is no official  package, so it is very difficult to use, not a standard, efficient way.

Perhaps Golang should like PHP/Python built-in some common operations,  Programming language is used to solve practical problems。

[snip]

Nate Finch

unread,
Jun 20, 2014, 8:06:50 AM6/20/14
to golan...@googlegroups.com, oju...@gmail.com
The number one answer to most questions about "how do I do X in Go?" is "write a loop".

Look for an element in a slice? Write a loop:

func contains(target val, slice []val) bool {
    for _, v := range slice {
        if v == target {
            return true
        }
    }
}

convert a slice to a map?  Write a loop:

m := make(map[string]val, len(slice))
for _, v := range slice {
    m[v.Field] = v
}

etc

Go doesn't have parametric polymorphism, so you can't write generic versions of these things.   (though see the standard library's package sort for a generic way to sort a slice).

There is very likely not going to be support for extreme shorthand versions of these operations any time soon.  However, most Go programmers don't mind.  These loops are trivial to write, and make it a lot more obvious what the behavior of your code will be.

foo.Contains(bar) looks like a simple function call, but is actually generally going to be O(n) processing time.

-Nate

DV

unread,
Jun 20, 2014, 3:09:46 PM6/20/14
to golan...@googlegroups.com
Hate to say this, but you're looking at the wrong language. 

Go isn't going to copy Python, it *definitely* isn't going to copy PHP (any feature in PHP should be considered immediately suspect and harmful until proven otherwise). 

Most of the operations you want can be written with a few simple loops and conditional checks. A library doesn't exist, because a general library for those operations would have to do reflection and thus kill both performance and static type checking. So it just won't happen. 

Many people like you have come here before, demanding Go to be more like C++/PHP/Python/ML/Haskell and they have all walked away disappointed. 

Whether you're technically correct about those features or not actually doesn't matter - you're still barking up the wrong tree. Go is not for you if you can't live without those features being built-in and if you're unable/unwilling to implement them yourself. 

Milan P. Stanic

unread,
Jun 21, 2014, 5:08:50 PM6/21/14
to golan...@googlegroups.com
On Fri, 2014-06-20 at 04:57, Larry Clapp wrote:
> I don't know PHP, but from what I've seen of it, it looks a lot like Perl.
> I know Perl fairly well.
>
> One of the key differences between Perl and Go (and possibly PHP and Go) is
> that Go has structs and Perl has only hashes (as far as named data
> structures)[1]. It's very important to not try to write Perl in Go (i.e.
> duplicating your Perl hashes in Go hashes, etc), but write more native,
> idiomatic Go, that takes full advantage of Go's structs and other native
> data structures.

From my PoV main difference is that the Go is strongly typed while most
'scripting' languages (Perl, PHP, Python etc.) are untyped (weak typed
or whatever) and I don't like to argue what typing means).
[...]

--
Best regards

Viktoriia Kapyrina Yelizarova

unread,
Nov 9, 2023, 12:35:38 PM11/9/23
to golang-nuts
Well, reflection is one of the things I love in language and it is a "must". 
 On the other hand, the automation is exactly what common libraries do, I can not find any reason to make a loops if they are repeated actions which might be implemented in general library.  array_intersect is a common example of automation. It makes no sense to right it again and again as it is common operation which works the same way.

Mike Schinkel

unread,
Nov 9, 2023, 9:50:03 PM11/9/23
to golang-nuts
Hi Vicktoriia,

Go has reflection:  https://pkg.go.dev/reflect

And in its various packages it also has many (though not all) of the types of functions mentioned above that PHP has:
Go does not have equivalent functions for working with PHP-style arrays because it does not support PHP style arrays. Few other languages (any?) do, in part I think because they are weird, and likely hard to make performant. But in Go you can simulate them with `map[any]any` I think.

Before Go, I spent around a decade working in PHP. After about 6 months I never wanted to work in PHP again.  One of the reasons PHP has so many array functions is that you cannot make low-level data manipulation functions performant, such as working character by character, or executing many loops. So PHP needs to have them in its standard library in order to be usable.

Go OTOH handles low-lever data manipulation performantly, so there is not nearly as much need to have every conceivable array operation in the standard library like there is in PHP.  Over the five years I've been coding in Go the Go team has added many functions to the standard library, including many of those from the links I included above. 

So It is quite possible the Go team will add a function to create an intersection and a union of slices to the standard lib at some point. And this is especially true since Generics are a recent addition and their inclusion allows functions to apply to many different data types. This means there is now arguable benefit for the Go team to add some of the functionality they are missing when compared to PHP.

But even if they do not, it would be trivial to write many of them and add them to your own package of functions, publish them to GitHub, and then they would also be just an import statement away.

If you are new to Go, allow yourself to get to know it for several months before judging it.  If you are anything like me, you will come to appreciate Go by leaps and bounds more than working with PHP. #fwiw

-Mike 

ahuigo

unread,
Nov 10, 2023, 1:06:42 AM11/10/23
to golang-nuts
Instead of reflect library. I'd like to recommend this generic library https://github.com/samber/lo.

It's very convenient than reflect, and I've been using it for a long time.

If you are not familiar about how to use it, ask GPT4(GPT3.5 does't know it)

Volker Dobler

unread,
Nov 10, 2023, 2:00:30 AM11/10/23
to golang-nuts
On Thursday, 9 November 2023 at 18:35:38 UTC+1 Viktoriia Kapyrina Yelizarova wrote:
]  array_intersect is a common example of automation. It makes no sense to right it again and again as it is common operation which works the same way.

Except that almost all implementations of array_intersect
either have strange edge case (NaNs), have unintuitive semantics
(function equality), are awkward to use (callback/predicate hell)
or are dangerous to use (hidden runtime explosion).

The _simple_ cases are simple, but these are _simple_ and
stuff like array_intersect  don't provide much benefit here.

V.
Reply all
Reply to author
Forward
0 new messages