Golang constant 2d array syntax fails [duplicate] - go

This question already has answers here:
Declare a constant array
(5 answers)
Closed 6 years ago.
I want to declare a constant golang 2d array (not slices), but I can't figure it out, having looked at the other golang comments on this issue.
type fooT [1][1]float64
const BAR fooT = {[1]float64 {.01}}
Gives the error fubar.go:5: syntax error: unexpected {. But the following compiles fine:
type fooT [1][1]float64
var BAR = fooT {[1]float64 {.01}}
First, I do not understand why I need to redeclare the underlying array redundantly, and it does seem golang compiler knows the type because it gives an error if I change it. But, why can I not make this array a const? it is R/O, not a global.
And, the syntax is cumbersome.

From the specs:
Constants
There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants.
IOW, in Go no {struct,array,slice,map,interface,pointer} constants exists.

Related

What does a pair of round brackets syntax expression mean in Go? [duplicate]

This question already has answers here:
Typecasting in Golang
(5 answers)
Closed 2 years ago.
Reading this https://github.com/go-pg/pg/wiki/Writing-Queries#select I see many times this expression:
(*Book)(nil)
Example:
count, err := db.Model((*Book)(nil)).Count()
What does it mean?
That is a type conversion. Assuming the db.Model function takes interface{}, it sends an nil interface of type *Book to the function.
To convert a value v to type Book, you'd write:
Book(v)
However, you cannot write Book(nil) because nil is a pointer and Book is not. If you had a type
type BookPtr *Book
Then you could've written BookPtr(nil). Extending that, you want to write *Book(nil), but that means *(Book(nil)) which is invalid, hence:
(*Book)(nil)
'nil' is to Go what NULL/null is to other languages like C#/Java, etc. The *Variable is just getting the pointer value for the Book object of Model.
So in this case, I believe what's happening here is that (*Book)(nil) is setting the pointer value of the Book object of the Model to nil(/null).
Hope this helps in some way. 😊
Good Resource: https://go101.org/article/nil.html

Why can a const initializer not be nil? [duplicate]

This question already has answers here:
Declare a constant array
(5 answers)
Closed 3 years ago.
I have a type A that is basically a simple map:
type A map[int32]struct{}
Now, I would like to have a special value of this type to be able to treat it a bit differently. I thought that it would be smart to use nil for this propose (additionally, this way, all non initialized variables of type A would have this value, and this is also what I would like to have):
const s A = nil
But I got
const initializer cannot be nil
Sure I can accept this and refactor my program in dozens of different ways. But I'm still wondering why it's impossible to initialize const to nil? There must be an architectural reason but I don't see it.
(Note that I prefer to "rename" nil instead of using it directly only because the name nil is not very intuitive in my case.)
I believe that it is because you can only have constants of type boolean, rune, integer, floating-point, complex, and string.
Docs: https://golang.org/ref/spec#Constants
Whereas nil is a zero-value for types pointer, interface, map, slice, channel and function
Docs: https://golang.org/ref/spec#The_zero_value
You can read more here https://blog.golang.org/constants

Assign multi-value to struct literal [duplicate]

This question already has answers here:
Multiple values in single-value context
(6 answers)
Closed 7 years ago.
Is there any way in Go to do this:
segment := Segment{
CumulativeDistanceMm: strconv.Atoi(record[9]),
Length: strconv.Atoi(record[1]),
LinkId: strconv.Atoi(record[8]),
SegmentId: strconv.Atoi(record[2]),
}
The error that I get is that strconv.Atoi returns multiple values so I can't assign it directly to the struct properties. If it was a variable I could use the underscore to ignore the second value. Can I do something similar for structs?
strconv.Atoi can fail and you have to deal with this failure. If such failures are absolutely impossible you would write a function func MustAtoi(s string) int which panics on failure and use that one in your struct initialization.
In Go doing some programming instead of using syntactical sugar or fancy syntax is common.
Most probably you should rethink your error handling.

Unsigned number ruby [duplicate]

This question already has answers here:
How to declare 8-bit unsigned integer in ruby?
(2 answers)
Closed 9 years ago.
I want a variable to hold a number that can't be assigned a negative number, so that myvar = -1 would just end up being 0. I can easily make my own class to do this, but does ruby already come with one?
No, you'll need to handle validation of the value on your own. Here's more info on the different ruby numerical types: http://www.techotopia.com/index.php/Ruby_Number_Classes_and_Conversions
You'll need a custom class to do it. Since Ruby is dynamically typed, you can't prevent a particular variable from holding a negative integer, or even a string for that matter.

Ruby Constants is there a way to have real constants ..? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Throw exception when re-assigning a constant in Ruby?
When we use a capital letter word in ruby, it is a constant: CONSTANT = "alive". When I modify this "supposed" constant, it gives an error, but modifies it anyway:
(irb):27: warning already initialized constant CONSTANT
=> "13".
This seems like an odd behavior. If I am designing a game and need a value to be constant, say: Cheatcode_health = true, and by accident, the value gets assigned as false or 0, it would be an unusual behavior. There could be lot of implications. In short is there a true constant in Ruby?
Ruby is a very permissive language. There's no way to raise an error if you re-assign a constant.
The only workaround is to create a custom method to assign values to constants and have this method do the check for you.
Other related questions:
Throw exception when re-assigning a constant in Ruby?
Can you ask ruby to treat warnings as errors?

Resources