2022-12-03
This commit is contained in:
33
chapter-2/03_const.go
Normal file
33
chapter-2/03_const.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// constants are used to give names to literals
|
||||
// There is no way to declare that a value is immutable
|
||||
// Use constants for hard-coded package level variables
|
||||
|
||||
// constants can be typed or untyped
|
||||
// leave them untyped for flexibility (can use an untyped int constant in a func that takes a float etc)
|
||||
// or hardcode them if needed
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
const x int = 10
|
||||
|
||||
const (
|
||||
idKey = "id"
|
||||
nameKey = "name"
|
||||
)
|
||||
|
||||
const z = 20 * 10
|
||||
|
||||
func main() {
|
||||
const y = "hello"
|
||||
|
||||
fmt.Println(x)
|
||||
fmt.Println(y)
|
||||
|
||||
x = x + 1
|
||||
y = "bye"
|
||||
|
||||
fmt.Println(x)
|
||||
fmt.Println(y)
|
||||
}
|
||||
Reference in New Issue
Block a user