How to Create Console Programs Using Go? 🐹
August 28, 2022 ¿Ves algún error? Corregir artículo
To create a console program with Go we mainly need to know 3 things:
- Get data entered by the user
- Process the information
- Print the processed information to the console
Now that we know what we need to know, let's begin step by step.
1. Getting user data from the console
In Go we'll use the standard library to get the user's information entered via keyboard to the console. We must declare our variable and its type in advance.
~fmt.Println("Enter your name:") var Name string fmt.Scanln(&Name)
2. Processing the information
We'll use a layered architecture to process our information obtained through the console.
database
database.go
controllers
controllers.go
services
service.go
main.go
go.mod
Inside the services folder we'll have all the services where we'll process the information. This will communicate with the controllers and with the database layer.
3. Print to console
In Go there's the "fmt" package which will help us print information to the console in our program. Let's look at 3 ways.
- Println : Prints and inserts a line break
- Print : Prints the provided information
- Printf : Prints with formatting
~name := "Carlos" fmt.Print("Welcome") fmt.Println("Hello I have a line break") fmt.Printf("My name is: %s", name)
With these basic concepts for building a console program and with basic programming concepts, we can start our project where we'll create our own travel planner.