In Go I use := for short variable declaration and = for assignment. How does EK9 handle this?
← Getting Started · Ref: Q1005
EK9 separates declaration from assignment just like Go, but with different symbols.
Go: name := "Steve" short declaration (creates variable)
EK9: name <- "Steve" declaration (creates variable)
Go: name = "Alice" assignment (updates variable)
EK9: name := "Alice" assignment (updates variable)
Notice the swap: Go's := (declare) maps to EK9's <- (declare). Go's = (assign) maps to EK9's := (assign).
EK9 also has : and = as aliases for :=
name := "Alice" assignment name: "Alice" same thing name = "Alice" same thing
And :=? for guarded assignment (like Go's 'if err != nil' pattern):
fallback :=? "default" only assigns if fallback is currently unset
Go's 'if err := f(); err != nil' pattern becomes an EK9 guard:
if result <- doSomething() useResult(result)
Example
defines module qa.gettingstarted.fromgodeclaration defines program FromGoDeclarationDemo() stdout <- Stdout() // Like Go: name := "Steve" (short declaration) name <- "Steve" stdout.println(name) // Like Go: name = "Alice" (assignment) name := "Alice" stdout.println(name) // Like Go: if err := f(); err != nil { ... } if greeting <- greetUser(name) stdout.println(greeting) defines function greetUser() as pure -> userName as String <- rtn as String: "Welcome, " + userName
Common mistakes
E50001 — In EK9, <- declares (like Go's :=) and := assigns (like Go's =). Using := before <- means the variable does not exist yet.
Incorrect:
name := "Steve" name = "Alice"
Correct:
name <- "Steve" stdout.println(name)
Other ways to ask this
- What is the EK9 equivalent of Go's := and = operators?
- I'm a Go developer. How do declaration and assignment work in EK9?
- How does EK9 separate declaration from assignment like Go does?
Coming from another language?
Go developers: your := becomes EK9's <-, your = becomes EK9's :=. The separation of declaration from assignment is the same concept, different symbols.
Keywords: assignment, go, migration, golang, short variable, declaration