Map string (key) to number (or any data) [closed] - go

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
In JavaScript I can do something like this:
const periods = {
'mercury': 0.2408467,
'venus': 0.61519726,
'earth': 1.0,
'mars': 1.8808158,
'jupiter': 11.862615,
'saturn': 29.447498,
'uranus': 84.016846,
'neptune': 164.79132,
};
const planetName = prompt();
const period = periods[planetName];
How can I do similar thing in go-lang?

You came very close to answering your own question. :)
Just put those tags on google.
package main
import (
"fmt"
)
func main() {
periods := map[string]float32{
"mercury": 0.2408467,
"venus": 0.61519726,
}
fmt.Println(periods["mercury"])
}

Related

Compare two structs like array in golang [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I am trying to compare 2 structs of the array type in Golang in order to know if having struct1 obtained from web scrapping is equal to struct 2, which is data fetched from the database.
It is the way that I have thought to be able to know if there has been a change between the external web and my database.
The structs is:
type Exchange struct {
Name string `gorm:"Column:name" json:"name"`
Buy float64 `gorm:"Column:buy" json:"buy"`
Sell float64 `gorm:"Column:sell" json:"sell"`
}
The result after consult is from scrapping:
&[{Dólar 38.5 41 } {Euro 38.82 43.57 } {P. Argentino 0.05 0.35 } {Real 6.95 8.95 }]
From web
&[{Dólar 38.5 41} {Euro 38.82 43.57} {P. Argentino 0.05 0.35} {Real 6.95 8.95}]
My Code:
fmt.Println(exchanges)
dbExchanges := getExchangesFromDB()
fmt.Println(dbExchanges)
if exchanges == dbExchanges {
fmt.Println("is equal")
} else {
fmt.Println("no is equal")
}
fmt.Println("Struct equal: ", reflect.DeepEqual(exchanges, dbExchanges))
Result:
no is equal
Struct equal: false
In the first if you are comparing the memory address of the two variables instead of theirs values. In the second if clause (using reflect.DeepEqual) you are comparing their values.

How does "or" short-circuiting and error work in Go? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
if true || 0/0 == 0 {
print()
}
If the first parameter is true, then the 0/0 wouldn't be evaluated.
Why does this return a DIVIDE BY ZERO error?
The divide by zero here is a compiler error, not a runtime error. Shortcutting only applies at runtime. If you change it to 0/x where x is set to zero, you won't get the error:
var x = 0
if true || 0/x == 0 {
print()
}
https://play.golang.org/p/7E9MMqUbnQm

How Do I Assign Values of 2 or more Fields in VBScript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi sorry for a basic question on VBScript but after scouring the internet. In a If statement I am trying to assign values to 2 fields but when I run this code which is part of an application it does not error neither does it work so I suspect I am doing wrong. Can anyone point me in the right direction as I am trying to assign values to CustomerInformation.CodeObject.Customer and. CustomerInformation.CodeObject.Carrier
Function CustomerInformation_OnLoad()
if (SystemVariables.CodeObject.Company = "D" or SystemVariables.CodeObject.Company = "T") and DispatchNoteDetails.CodeObject.Area = "UK" then
if CustomerInformation.CodeObject.Customer = "AAE02" then
CustomerInformation.CodeObject.Carrier = "Customer collects" and CustomerInformation.CodeObject.CarrierURN = "AA Driver"
end if
end if
End Function
Hi thanks all for the tips.
The code worked when changing to the following:
Function CustomerInformation_OnLoad()
if (SystemVariables.CodeObject.Company = "D" or SystemVariables.CodeObject.Company = "T")then
if CustomerInformation.CodeObject.Customer = "AAE02" then
CustomerInformation.CodeObject.Carrier = "Customer collects"
CustomerInformation.CodeObject.CarrierURN = "AA Driver"
end if
end if
End Function
Thanks again for the tips.

How do YOU handle bad parameters in functions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Let's say you have some function that has one int parameter that can be good or bad. Let's say that it's bad when it's less than 5. And if it's bad you should get out of function. I think you already made up that function in your mind. Now tell me which of these functions is what you would've written.
1.
void abc(int a)
{
if (a < 5) return;
//...
}
2.
void abc(int a)
{
if (a >= 5)
{
//...
}
}
This may sound like a really stupid question. But I often have hard time deciding between these two lol.
I prefer the first way:
if a < 5
// return error or throw exception
To me it looks like some kind of "guard". Also you should handle this "bad" variable somehow (return error, throw exception) and it will be harder to follow if this is in some else block somewhere in the function.

Code improvement [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Here is my code sample, let me know if it can be further improved?
excludedb = if File.exist?(arg)
IO.read(arg).split(',').map { |db_name| db_name.strip }.delete_if { |db_name| db_name == "" }
else
["master", "model", "sybsystemdb", "sybsystemprocs", "tempdb", "sybsecurity", "pubs2", "pubs3", "dbccdb", "sybmgmtdb"]
end
Here's a couple of tiny improvements.
You can replace
.map { |db_name| db_name.strip }
with
.map(&:strip)
And also you can use string array literal
%w{master model sybsystemdb}

Resources