Avatar ·

How to make a one-line if-else in Go? Ternary operator

Hello everyone! I just started learning Go after JavaScript and Python, and I ran into a problem. In those languages, I'm used to writing short conditional expressions in one line, for example:\n\nIn JavaScript: `const status = age >= 18 ? "adult" : "child";`\nIn Python: `status = "adult" if age >= 18 else "child"`\n\nThis is very convenient for simple assignments. But in Go, I can't find an analog. I tried to write something like:\n\n```go\nstatus := age >= 18 ? "adult" : "child"\n```\n\nBut the compiler gives an error. I tried different options, but nothing works. Does Go really have no ternary operator at all? How do you write simple conditions in one line then? I have to write entire if-else blocks even for elementary things, and the code becomes very cumbersome.\n\nFor example, I want to assign a value to a variable based on a condition, but I have to do it like this:\n\n```go\nvar status string\nif age >= 18 {\n status = "adult"\n} else {\n status = "child"\n}\n```\n\nThis is 5 lines instead of 1! Is there really no way to make it shorter? Maybe there is some trick or idiomatic way in Go to write such simple conditions compactly?