Загрузка...

Reading User Input in Go

So far, your programs have mostly worked with values defined directly in code. Many real programs need to accept input from the user, such as names, numbers, or commands typed into the terminal. Go provides simple functions in the fmt package for reading input from standard input, usually the terminal. The most common one is fmt.Scan. In this Go Byte, you'll learn how to read user input using fmt.Scan and understand how it works.

What You'll Learn:
- How to read user input from the terminal using fmt.Scan
- Why you need to pass variable addresses using the address operator
- How to read multiple values in a single scan operation
- How fmt.Scan handles space-separated input
- The basics of creating interactive command line programs

Conceptual Explanation:
The fmt.Scan function reads space-separated values from standard input, which is usually the terminal where the user types. Unlike printing output, reading input requires you to pass the address of variables using the address operator. This is because fmt.Scan needs to modify the variable values by storing what the user types. You can read multiple values at once by passing multiple variable addresses, and fmt.Scan will read each value separated by whitespace. The function stops reading for each value when it encounters whitespace, so it works well for simple inputs like names and numbers.

How It Fits:
Reading user input is a fundamental skill for building interactive programs and command line tools. While most examples start with hardcoded values, real programs often need to accept configuration, commands, or data from users at runtime. Understanding fmt.Scan gives you the foundation for creating programs that respond to user input. This lesson builds on your knowledge of variables, addresses, and the fmt package, showing you how to combine printing prompts with reading responses.

Key Takeaways:
- fmt.Scan reads space-separated input from standard input
- You must pass variable addresses using the address operator
- Multiple values can be read by passing multiple variable addresses
- fmt.Scan reads until whitespace for each value
- This is the foundation for interactive command line programs

Try the challenge from the video before checking the solution.

Links:
Lesson folder on GitHub: https://github.com/GaryClarke/go-bytes/tree/main/reading-user-input
Build your first Go app with me: https://www.garyclarke.tech/p/build-your-first-go-app
Join the mailing list for new lessons and resources: https://content.garyclarke.tech/go-bytes-signup
Follow on X/Twitter: https://twitter.com/garyclarketech
Connect on LinkedIn: https://www.linkedin.com/in/garyclarketech/

Subscribe for more Go lessons.

#golang #go #programming #coding #learnprogramming #gobytes

Видео Reading User Input in Go канала Gary Clarke
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять