Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created March 30, 2012 13:41
Show Gist options
  • Save jpoehls/2251637 to your computer and use it in GitHub Desktop.
Save jpoehls/2251637 to your computer and use it in GitHub Desktop.
Duplicate const values in #golang with iota
package main
import "fmt"
// iota is reset to 0 at every const declaration
const (
Zero = iota // within a const declaration, iota is incremented for every line
One // default value is set to iota, which is now 1
Two = 2 // exlicitly set to 2
TwoAgain // default value is set to iota, which is now 2 creating a duplicate 2
)
func main() {
fmt.Printf("Zero: %d\n", Zero)
fmt.Printf("One: %d\n", One)
fmt.Printf("Two: %d\n", Two)
fmt.Printf("TwoAgain: %d\n", TwoAgain)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment