Linked Questions
12 questions linked to/from What is the idiomatic Go equivalent of C's ternary operator?
173
votes
12
answers
344k
views
How to do one-liner if else statement? [duplicate]
Please see https://golangdocs.com/ternary-operator-in-golang as pointed by @accdias (see comments)
Can I write a simple if-else statement with variable assignment in go (golang) as I would do in php? ...
0
votes
0
answers
162
views
What is Python's "and/or hack" and how can it be emulated in Go? [duplicate]
I'm porting an Excel-style formula parser from Python to Go and came across this comment in the function definition of the token stack:
def token(self):
# Note: this uses Pythons and/or "hack"...
94
votes
14
answers
135k
views
Ternary operator ?: vs if...else
In C++, is the ?: operator faster than if()...else statements? Are there any differences between them in compiled code?
33
votes
3
answers
42k
views
How can I conditionally set a variable in a Go template based on an expression which may cause an error if not wrapped with an if statement
Question
How do I do something like this:
{{ $use_ssl := (ne $.Env.CERT_NAME "") }}
where $.Env.CERT_NAME may be nil/undefined. If it is nil, it gives this error:
at <ne $.Env.CERT_NAME "">: ...
8
votes
2
answers
3k
views
Stimulate code-inlining
Unlike in languages like C++, where you can explicitly state inline, in Go the compiler dynamically detects functions that are candidate for inlining (which C++ can do too, but Go can't do both). Also ...
7
votes
1
answer
10k
views
How do I turn a bool into an int? [duplicate]
I'm used to C/Java, where I could use ?: as in:
func g(arg bool) int {
return mybool ? 3 : 45;
}
Since Go does not have a ternary operator,
how do I do that in go?
2
votes
1
answer
7k
views
Why Go doesn't have a ternary conditional operator [closed]
I'm relatively new to Go, and in a bid to catch on quickly I tried rewriting some of my JavaScript(NodeJS) code in Go. Recently I hit sort of a stumbling block, where I found out that Go doesn't have ...
1
vote
2
answers
4k
views
Handling nil values in a map
I want to create a struct with the values in a map.
Here's a snippet:
log := &Log{
Facility: parts["facility"].(int),
Severity: parts["severity&...
0
votes
3
answers
3k
views
How to make variable assignment with "or"
Is it possible, and how to make one-line (or just short) var assignment with such logic:
a := b (or if b is false, nil or undifined, then a equals to) "hello"
I try to make:
a := b | "hello"
but ...
1
vote
2
answers
3k
views
Assign value to field in struct if empty [duplicate]
I have a struct defined
type data struct {
invitecode string
fname string
lname string
}
which I populate from retrieving form data after parsing
...
r.ParseForm()
new user := &data{
...
1
vote
1
answer
2k
views
Does golang have the equivalent of this Python short circuit idiom `z = x or y`
In Python z = x or y can be understood as assign z as if x is falsey, then y, else x, is there a similar idiom in golang?
Specifically the two variables on the right are strings, I'd like to assign ...
-4
votes
1
answer
466
views
A Sprintf with 3 arguments of different pointers type that can be nil. Ternary operator being unavaialble, how to avoid writing tens of lines? [closed]
I have this string to create with a Sprintf
message := fmt.Sprintf("Unit %s has a Level of %v, but is of Category %v",
*entity.Name, *entity.LevelCode, *entity.CategoryCode)
In entity, ...