DEV Community

ShellRean
ShellRean

Posted on • Updated on

Golang basic: Input Output

There is 2 type of interface, GUI and the second one is CLI(Command Line Interface) it just show you text. The purpose of write code is to get an input and give output. each programming language has their way to do that. if you from c it gonna be easy cause the way is similar. let's code

Output Program

create first program it will be good if we start with hello world program it simple and pretty cool. let's we create file main.go open it in your love code editor.
write code below.

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

Enter fullscreen mode Exit fullscreen mode

First we declare main as package name it will tell the program what package is it. program will look for package main to run program first.

import key to import package to go's file you can't use fmt if you not import it first. there is many go packages that you can using it.

func main() is a function main. what code you write in this block it will be run first.

fmt.Println("Hello World")to use package that you have imported before(fmt) just write what package is, for example fmt.(dot) and following what function you need to call. this line will print out "Hello World" to your console.

to run our code, go to where the file was saved and open te terminal.

go run main.go
Enter fullscreen mode Exit fullscreen mode

it will show you text

Hello World
Enter fullscreen mode Exit fullscreen mode

Input Program

get input user from console is like a magic what your input, it will show to the console let's we edit the file main.go

package main

import "fmt"

func main() {
  var name string;

  fmt.Printf("Write your name : ")
  fmt.Scanf("%s", &name)

  fmt.Printf("Hello my name is %s", name)
}
Enter fullscreen mode Exit fullscreen mode

var name string it will declare variable name as string. We will learn about data type and variable next.

There is new function we use its Scanf it will get user's input. %s it will tell the program that the data type of variable is string and &name is variable that where input use will be stored.
don't forget to add & before the variable name.

fmt.Printf as before we print program with Println but it is without formatting and Printf will format what you want to show to user.
"Hello my Name is %s" the %s will replaced by name so what you write in name it will change %s.

let's we run program.

go run main.go
Enter fullscreen mode Exit fullscreen mode

we will get output

Write your name : 
Enter fullscreen mode Exit fullscreen mode

Input your name

Write your name: shellrean
Enter fullscreen mode Exit fullscreen mode

The output will be

Hello my name is shellrean
Enter fullscreen mode Exit fullscreen mode

Conclusion

We have write simple input and output program with go, we have use Println, Printf, Scanf function from fmt package to create our simple program. dont forget to change the code if you have and idea to improve the code.

challanger:
Write code that will show

I love learn go
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
njoxpy profile image
Godbless Nyagawa

Thanks, I have been struggling to understand how we can get user input but for now somehow.

Questions: How about people are using buffer.io module to get user input and how does it work? is there any difference with the above instance.

Thanks.