← कोर्स पर वापस जाएँ: Go from scratch

Constants, iota and week 1 review

वीडियो जल्द आ रहा है

A constant is a value fixed at compile time that cannot change while the program runs. It is declared with the word const. Constants are useful to name values that have meaning, such as a tax rate, the maximum number of retries or an application name. Using constants instead of loose numbers in the code (so-called magic numbers) makes the program more readable and means changing a value is a matter of touching a single place.

go
package main

import "fmt"

const taxRate = 0.21
const (
	maxRetries = 3
	greeting   = "hello"
)

func main() {
	net := 100.0
	gross := net * (1 + taxRate)
	fmt.Println(greeting, maxRetries, gross)
	// taxRate = 0.10  // compile error: cannot assign to taxRate
}

Untyped constants

A special feature of Go is that constants may have no fixed type. In const taxRate = 0.21 the value is an untyped floating constant and adapts to the context where it is used: it can combine with a float32 or a float64 without conversions. If you want to fix the type, write it: const limit int64 = 100. Untyped constants also have practically unlimited precision at compile time, which allows exact calculations with huge numbers before they are assigned to a variable.

Enumerations with iota

Go has no enum keyword, but it offers iota: inside a const block, iota is 0 on the first line and grows by one on each following line. If you omit the expression on later lines, the previous one is repeated. Combined with your own type, it creates clear enumerations. We create a type with type Weekday int, which is a new type based on int, and give it constants with iota. This way the compiler tells a Weekday apart from any plain int and the code documents its intention.

go
package main

import "fmt"

type Weekday int

const (
	Monday Weekday = iota // 0
	Tuesday               // 1
	Wednesday             // 2
)

const (
	_  = iota             // skip 0
	KB = 1 << (10 * iota) // 1024
	MB                    // 1048576
)

func main() {
	fmt.Println(Monday, Wednesday, KB, MB)
}

Week review

This week you installed Go, created a module, wrote a program with package main and func main, and ran it with go run and go build. You learned to declare variables with var and with :=, the basic types, the zero value and explicit conversions. Today you added constants and iota. Before moving on to week 2, practice with the exercise and answer the quiz: it is the best way to check that the concepts are solid.

  • Every executable program has package main and func main.
  • Unused imports and variables are compile errors.
  • := only inside functions; var anywhere.
  • There is no automatic conversion between numeric types.
  • Constants are fixed at compile time; iota generates sequences.

जो सीखा उसे परखें

प्रश्न 1 / 6

Which function marks the entry point of an executable Go program?

✎ मेरे नोट्स

केवल इस ब्राउज़र में सहेजा गया। खाते में रखने के लिए साइन इन करें।0/5000 अक्षर

← पिछलाअगला →