Go - Basic Input/Output

Go is like the perfect blend of simplicity and power. It’s clean, fast, and built for scale — making it a solid choice whether you’re cracking coding interviews or building real-world backends.Feels almost like Python but compiled and fast like C++.
How do we write basic input and output?
In Go, we import packages to use specific functionality like reading input or printing to console.
import fmt, stands for format, it provides input like println and scan for input and output
func main is a main function, which acts like entry point.
Println prints output with a new line, while print gives us no newline.
we can use “\n” for line breaks
fmt.Scan or fmt.Scanln is used to take input from the user.
package main
import "fmt"
func main() {
fmt.Println("Hello, World! This is Ishaa and she is learning go")
}
//Output
Check out this code to take multiple inputs
&x and &y are pointers to variables where the input will be stored.
package main
import "fmt"
func main() {
var x, y int
fmt.Print("Enter two numbers: ")
fmt.Scan(&x, &y)
fmt.Println("Sum:", x+y)
}
Go doesn’t allow unused imports or variables — it will throw a compile-time error.
you can import multiple packages like given below
import (
"fmt"
"math"
)
- Go doesn’t use public, private, or protected keywords like other languages. Capitalizing a name makes it public/exported, lowercase means it stays private to the package.If something starts with a lowercase letter, it’s meant to be used only within the same package.
Functions
A function can take zero or more arguments. Here is how we write functions in go, notice how type is written after variable declaration.
func add(x int, y int) int {
return x + y
}
you can also write x int, y int
as x,y int
since both the variables have the same type int.
Now try writing a swap function before moving ahead. this is in int, try swapping the hello world.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
a,b := swap(8,9)
fmt.Println(a,b)
}
func swap(x,y int)(int, int){ // declare return type
return y,x
}
Naked Return
In Go, you can also name its return values which is good for small code but for long ones readability is compromised. A return
statement without arguments returns the named return values. Check out the below code how sum is returned.
package main
import "fmt"
func main() {
fmt.Println(add(1,3)) //4
}
func add(x, y int) (sum int) {
sum = x + y
return
}
Variables
Observe how do we write variables in go, if the value is not initialized it takes up the type of the variable.
Inside a function, the :=
short assignment statement can be used in place of a var
declaration with implicit type.
package main
import "fmt"
var i, j int = 1, 2
func main() {
var c, python, java = true, false, "no!"
name:= "Isha"
fmt.Println(i, j, c, python, java, name)
}
Data Types in Go
🔹 Numeric Types
Go provides both signed and unsigned integers, floating points, and complex numbers.
Integers:
Signed:
int
,int8
,int16
,int32
,int64
Unsigned:
uint
,uint8
,uint16
,uint32
,uint64
Type | Size | Range |
int | 32/64-bit | Platform dependent |
int8 | 8-bit | -128 to 127 |
uint8 | 8-bit | 0 to 255 (byte ) |
✅ Use
int
unless you have a specific reason for another size.
Floating-Point:
float32
,float64
Complex Numbers:
complex64
,complex128
Aliases:
byte
=uint8
rune
=int32
(used for Unicode)
🔹 String Type
Represents a sequence of bytes. Strings in Go are immutable.
s := "hello"
fmt.Println(s[0]) // prints byte value of 'h'
fmt.Println(string(s[0])) // prints 'h'
🔹 Boolean Type
Simple true/false values.
var b bool = true
📄 Composite Types
→ Arrays
Fixed-length collections.
var a [3]int = [3]int{1, 2, 3}
→ Slices
Dynamic-length arrays.
s := []int{1, 2, 3}
→ Maps
Key-value store.
m := map[string]int{"a": 1, "b": 2}
→ Structs
Custom data types.
type Person struct {
Name string
Age int
}
🧰 Reference Types
→ Pointers
Store memory addresses.
x := 10
p := &x
→ Functions
First-class citizens in Go.
func add(a, b int) int {
return a + b
}
🧬 Interface Types
Define behavior by specifying method sets.
type Shape interface {
Area() float64
}
Any type implementing Area()
can be used as a Shape
.
🔎 Zero Values
Variables without initial values get default values:
Numbers:
0
Boolean:
false
String:
""
Reference types:
nil
var i int // 0
var b bool // false
var s string // ""
✨ Type Inference
Go can infer types when using :=
:
package main
import "fmt"
func main() {
f := 3.14 // f is float64
i := 42 // i is int
c := 1 + 2i // c is complex128
fmt.Printf(" f: %T, i: %T, c: %T\n", f, i, c)
}
↺Type Conversions
The expression T(v)
converts the value v
to the type T
.
Type Inference - Go is statically typed, but it can infer the type of a variable from the value you assign to when you don’t explicitly say the type.
package main
import "fmt"
func main() {
i := 42
f := float64(i)
u := uint(f)
fmt.Printf(" f: %T, i: %T, u: %T\n", f, i, u)
}
Constants
Constants are declared using the const keyword. Constants cannot be declared using the :=
syntax. Constants can be of numeric, string, or boolean types.
package main
import "fmt"
const Pi = 3.14
func main() {
const World = "Ishaa"
fmt.Println("Hello", World)
fmt.Println("Happy", "Coding")
}
Now that you understand how to take input and print output in Go, you’re ready to jump into writing basic problems.
Subscribe to my newsletter
Read articles from Isha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
