0

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 getting an error: "operator | not defined on string".

so I am from JS, and trying to implement:

const a = b || "hello";

but in a "Go-way"

As it is statically typed Lang, I meant that if b == "" (empty string)

Adrian
  • 42,911
  • 6
  • 107
  • 99
Maxim
  • 475
  • 1
  • 5
  • 15
  • 3
    This is go, not JS. Go is statically typed. So at that point, b cannot be undefined, and it can either be nil (which means it is a pointer or one of the reference types), or false, which means it is a bool. So what you're trying to do is not possible. – Burak Serdar Oct 20 '19 at 20:17
  • 1
    it does not make much sense in go. Mostly because values are type invariant. Meaning, if `b` is a string, then it can not be nil, false, undefined or nan or another defined type, it got to be a string. As for smart declarations and syntactic sugar, Go will not please you much. Don't fight with it. –  Oct 20 '19 at 20:22
  • 2
    Given that your goal [is to default an env var](https://stackoverflow.com/questions/58476932/how-to-make-variable-assignment-with-or-in-golang#comment103286843_58477058), see [How to assign default value if env var is empty?](https://stackoverflow.com/questions/40326540/how-to-assign-default-value-if-env-var-is-empty) – Charlie Tumahai Oct 20 '19 at 21:10
  • Cerise Limón, well, in this particular case, yes, but generally, I've been searching for needed syntax(but as many said, it is not possible in the language) – Maxim Oct 21 '19 at 05:25

3 Answers3

5

There is no Go syntax like this. Use an if statement.

The operands of | must be integers. The operands of || must be booleans.

What's more, a string cannot be false or nil.

Given that OP's actual goal is to supply a default for an environment variable, the test should be on the boolean return value from os.LookupEnv, not the string value. Use a function like this:

func getenv(key string, def string) string {
     val, ok := os.LookupEnv(key)
     if !ok {
         val = def
     }
     return val
}
MonteDude
  • 159
  • 3
2

I had a similar desire when defining default values to Env variables.

The way I solved it was to define a function to do it, like this:

func getenv(key, fallback string) string {
    value := os.Getenv(key)
    if len(value) == 0 {
        return fallback
    }
    return value
}

When I call this function, it looks just as concise as a "or" would be:

port := getenv("PORT", "8000")

You could define your own functions to your use cases.

Auyer
  • 2,693
  • 3
  • 15
  • 32
0

I think doing your own function is great but golang has its own way for managing flags here is an example

import (
    "flag"
)

func main() {    
    env := flag.String("env", "dev", "description")
}

so here the default value for env is dev and you run and change it by

go run main.go -env=prod

regards