Go Package slices: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=External= * https://pkg.go.dev/slices =Internal= * Standard library =Overview=") |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
=Internal= | =Internal= | ||
* [[Go_Language_Modularization#slices|Standard library]] | * [[Go_Language_Modularization#slices|Standard library]] | ||
* [[Go_Slices#The_slices_Package|Go Slices]] | |||
=Overview= | =Overview= | ||
=Sorting= | |||
==<tt>Sort()</tt>== | |||
{{External|https://pkg.go.dev/slices#Sort}} | |||
<code>Sort()</code> <font color=darkkhaki>inplace</font> sorts a slice of any ordered type in ascending order. When sorting floating-point numbers, NaNs are ordered before other values. | |||
<syntaxhighlight lang='go'> | |||
s := []int{0, 42, -10, 8} | |||
slices.Sort(s) | |||
</syntaxhighlight> | |||
==<tt>Contains()</tt>== | |||
{{External|https://pkg.go.dev/slices#Contains}} | |||
<syntaxhighlight lang='go'> | |||
s := []int{0, 42, -10, 8} | |||
slices.Contains(s, 0) | |||
</syntaxhighlight> | |||
<code>Contains()</code> behavior in presence of alias types: <code>Contains()</code> will return <code>true</code> regardless of an instance of the alias type, or the original type is presented, assuming that the original instance type matches: | |||
<syntaxhighlight lang='go'> | |||
type StringAlias string | |||
s := []StringAlias{StringAlias("a")} | |||
slices.Contains(s, StringAlias("a")) # returns true | |||
slices.Contains(s, "a") # also returns returns true | |||
</syntaxhighlight> | |||
<font color=darkkhaki>However, IntelliJ does not like <code>slices.Contains(s, "a")</code> on some occasions, it seems that it wants <code>StringAlias("a")</code>, investigate.</font> |
Latest revision as of 03:23, 19 August 2024
External
Internal
Overview
Sorting
Sort()
Sort()
inplace sorts a slice of any ordered type in ascending order. When sorting floating-point numbers, NaNs are ordered before other values.
s := []int{0, 42, -10, 8}
slices.Sort(s)
Contains()
s := []int{0, 42, -10, 8}
slices.Contains(s, 0)
Contains()
behavior in presence of alias types: Contains()
will return true
regardless of an instance of the alias type, or the original type is presented, assuming that the original instance type matches:
type StringAlias string
s := []StringAlias{StringAlias("a")}
slices.Contains(s, StringAlias("a")) # returns true
slices.Contains(s, "a") # also returns returns true
However, IntelliJ does not like slices.Contains(s, "a")
on some occasions, it seems that it wants StringAlias("a")
, investigate.