Give us const arrays please :)

476 views
Skip to first unread message

Vladimir Efanov

unread,
Apr 11, 2021, 7:33:47 PM4/11/21
to golang-nuts
Hi Go-Masters

It will be very useful to get constant arrays in Golang.
Programmers asked about it for years .

For now we all should write not exported array definitions
and exported functions to get read access to it.

What we need
const MyArray = [n]T = {...} or initMyArrayFunc()

What we have
// -----------------------------------
var ws = [256]bool{' ': true, '\t': true, '\n\: true, '\r\: true}
// IsWhitespace checks 
func IsWhitespace(c byte) bool {
    return whitespace[c]
}
// -----------------------------------
var letter = [256]bool{
    'A': true, 'B': true, 'C': true, 'D': true, 'E': true, 'F': true, 
    ...
}
//  IsLetterASCII checks a start of an ASCII identifier
func  IsLetterASCII(c byte) bool {
    return letter[c] // 'A'-'Z', 'a'-'z', '_'
}
 and so on ...



Kurtis Rader

unread,
Apr 11, 2021, 8:47:31 PM4/11/21
to Vladimir Efanov, golang-nuts
This has been formally requested several times. For example:


I'm also willing to bet you are basing your request on experience with a language such as C. Adopting ideas from other languages is fine but blindly, and simplistically, shoehorning a feature from another language almost never has a satisfying result.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/01f4a524-9463-4770-8f9d-a17962cd5ba7n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Henry

unread,
Apr 12, 2021, 4:04:11 AM4/12/21
to golang-nuts
The example you gave is actually a map[rune]bool in Go, and not an array. I don't think C's array work like that too. I think you are confusing Go with other languages. In addition, arrays are rarely used in Go. Many people prefer using slices.

wagner riffel

unread,
Apr 12, 2021, 8:17:05 AM4/12/21
to Henry, golang-nuts
On Mon Apr 12, 2021 at 5:04 AM -03, Henry wrote:
> The example you gave is actually a map[rune]bool in Go, and not an
> array. I

It's a Go array in form of a composite literal, see
https://golang.org/ref/spec#Composite_literals

Henry

unread,
Apr 12, 2021, 10:24:43 AM4/12/21
to golang-nuts

Thanks for the information. I did not know that. It's good to learn something new. :)

tapi...@gmail.com

unread,
Apr 13, 2021, 7:03:14 AM4/13/21
to golang-nuts
Reply all
Reply to author
Forward
0 new messages