Golang / GoLang Basics Interview Questions
How do constants and iota work in Go?
Constants are declared with const and must be assigned a value that is computable at compile time — no function calls or runtime values. The iota identifier provides an automatically incrementing integer within a const block, resetting to 0 at the start of each new block.
// Simple constants
const Pi = 3.14159
const AppName = "MyService"
// iota: auto-incrementing integer, resets at each const block
type Weekday int
const (
Sunday Weekday = iota // 0
Monday // 1
Tuesday // 2
Wednesday // 3
Thursday // 4
Friday // 5
Saturday // 6
)
// iota with bit-shifting — perfect for flag constants
type Permission uint
const (
Read Permission = 1 << iota // 1 (001)
Write // 2 (010)
Execute // 4 (100)
)
userPerms := Read | Write // 3 — can read and write
// iota with expressions
const (
_ = iota // skip 0
KB = 1 << (10 * iota) // 1 << 10 = 1024
MB // 1 << 20
GB // 1 << 30
)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
