-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Description
Go Programming Experience
Novice
Other Languages Experience
Java, Python, Kotlin, JS
Related Idea
- Has this idea, or one like it, been proposed before?
- Does this affect error handling?
- Is this about generics?
- Is this change backward compatible? Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit
Has this idea, or one like it, been proposed before?
No, but not 100% sure
Does this affect error handling?
No.
Is this about generics?
No.
Proposal
This is a backward-compatible proposal to allow branching statement like if or switch to be used as an expression to be used for assignments.
This should reduce verbosity of code while keeping the impact minimal. It can also be used by static analysis tools to find unhandled scenarios with iota transformations.
Also, it would inadvertantly mean that go would have a ternary operator.
Consider the following code to find the maximum of two numbers:
package main
import "fmt"
func main() {
a := 1
b := 2
var max int
if a > b {
max = a
} else {
max = b
}
fmt.Printf("max: %d\n", max)
}with an if expression, it can be simplified to:
package main
import "fmt"
func main() {
a := 1
b := 2
max := if a > b {
a
} else {
b
}
fmt.Printf("max: %d\n", max)
}Some similar proposals would get an alternative syntax to be implemented:
Language Spec Changes
No response
Informal Change
No response
Is this change backward compatible?
Mostly. Haven't though about the whole picture
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
It would reduce the verbosity of the code and add an existing language feature of Python and Kotlin.
Cost Description
No response
Changes to Go ToolChain
gofmt, gopls, vet.
Performance Costs
No response
Prototype
No response