DAY 1 - Writing Your First Program in Go
Let’s write our very first program in Go!
Example: Hello World
package main
import "fmt"
func main() {
fmt.Println("Hello Developers!!")
}
I think you all guessed the output correctly 😄
How to run this code
Open your terminal, navigate to the folder where your Go file is located, and run:
go run file_name.go
Tip: If you saved the file as
main.go, run:go run main.go
⚠️ Common error: package command-line-arguments is not a main package
If you see an error like:
package command-line-arguments is not a main package
it usually means you haven’t initialized a Go module in the folder. In modern Go, projects are organized into modules. To initialize a module, run the following command inside your project folder:
go mod init module_name
You can choose any module_name (e.g., your repository path). This creates a go.mod file and enables dependency management.
Basic Intro to Go Modules: Go modules are the collection of packages. Here others will import your module when published using the module_name(repo path) that you have given.
Go Modules (contains packages) > Packages(contains code) > go code
After go mod init, run the program again:
go run file_name.go
Expected output:
Hello Developers!!
Hooray! 🎉 Your first Go program executed successfully.
Understanding the code
package main
Themainpackage defines an executable program. The Go tool expects amainpackage with amain()function for runnable programs.func main()
Themainfunction is the entry point of the program (similar to C/C++). Execution starts here.import "fmt"
Imports the standardfmtpackage, which provides formatted I/O functions.
About the fmt package
fmt is a standard package used for printing and formatting text in Go.
Common fmt functions:
fmt.Print("Hi") // Prints text as-is (no newline)
fmt.Println("Hello") // Prints text followed by a newline
fmt.Printf("Hi %s", "Dev") // Prints formatted string with placeholders
fmt.Sprintf("Hi %s", "Dev") // Returns the formatted string instead of printing
Note: Go does not allow unused imports or unused variables. If something is declared/imported but not used, the compiler will raise an error. This helps keep code clean.
Example program — Exploring fmt and input
Below is a complete example demonstrating fmt functions and simple user input with fmt.Scan. Save this as fmt_examples.go and run it.
package main
import "fmt"
func main() {
// Print without newline
fmt.Print("Hello")
fmt.Print(" Developers!") // Output: Hello Developers!
// Print with newline
fmt.Println("Welcome to Go programming!")
// Using Printf for formatted output
name := "Siva"
age := 24
fmt.Printf("My name is %s and I am %d years old.", name, age)
// Using Sprintf to return a formatted string
message := fmt.Sprintf("Hello, %s! You’re learning Go.", name)
fmt.Println(message)
// Taking input from the user
var user string
fmt.Print("Enter your name: ")
// fmt.Scan reads space-separated tokens from standard input.
// To read a full line with spaces, you can use bufio.NewReader + ReadString('\n').
fmt.Scan(&user)
fmt.Printf("Nice to meet you, %s!", user)
// Demonstrating various verbs in Printf
pi := 3.14159
fmt.Printf("Integer: %d", age)
fmt.Printf("String: %s", name)
fmt.Printf("Float: %f", pi)
fmt.Printf("Boolean: %t", true)
fmt.Printf("Go-syntax representation: %#v", struct{ A int }{A: 1})
}
Notes on reading input
fmt.Scan(&var)reads space-separated tokens. It stops at whitespace.To read a full line (including spaces), use
bufio.NewReader(os.Stdin):reader := bufio.NewReader(os.Stdin) line, _ := reader.ReadString('\n')(Don't forget to
import "bufio"andimport "os"when usingbufio.)
🎯 Key takeaways
Every Go program that is an executable should have
package mainand amain()function.Use
go mod initto initialize a module in your project directory.The
fmtpackage is the standard way to handle simple I/O and formatted strings.Go enforces no unused imports or variables — this encourages cleaner code.
✨ What's next?
In the next post (Day 2), we'll explore variables, constants, and data types in Go. We’ll see how Go’s type system helps prevent common bugs and how to use types effectively.
#GoLang #Backend #90DaysOfGo #Microservices #Development #SoftwareDevelopment #GoProgramming #BackendDevelopment #APIs #DevJourney #Programming