Linked Questions

173 votes
12 answers
344k views

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? ...
thoroc's user avatar
  • 3,667
0 votes
0 answers
162 views

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"...
leylandski's user avatar
94 votes
14 answers
135k views

In C++, is the ?: operator faster than if()...else statements? Are there any differences between them in compiled code?
Xirdus's user avatar
  • 3,097
33 votes
3 answers
42k views

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 "">: ...
Tyler Rick's user avatar
  • 9,560
8 votes
2 answers
3k views

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 ...
user avatar
7 votes
1 answer
10k views

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?
jorgbrown's user avatar
  • 2,061
2 votes
1 answer
7k views

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 ...
imadu's user avatar
  • 99
1 vote
2 answers
4k views

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&...
Élodie Petit's user avatar
0 votes
3 answers
3k views

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 ...
Maxim's user avatar
  • 645
1 vote
2 answers
3k views

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{ ...
driftavalii's user avatar
  • 1,339
1 vote
1 answer
2k views

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 ...
cerberos's user avatar
  • 8,083
-4 votes
1 answer
466 views

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, ...
Marc Le Bihan's user avatar