Go (Golang) / Introduction to Go
Writing and Running Your First Go Program
In this tutorial, we'll create your first Go program. We'll cover how to write, compile, and run a simple 'Hello, World!' program in Go.
Section overview
5 resourcesCovers the fundamentals of Go, its history, and how to set up a development environment.
Writing and Running Your First Go Program
Introduction
In this tutorial, we will walk through the process of creating your first program in Go, a powerful and efficient language developed by Google.
By the end of the tutorial, you will know how to:
* Write a simple program in Go
* Compile your Go code
* Run your Go program
Prerequisites
Before starting this tutorial, you should have Go installed on your computer. If you haven't, you can download it from the official Go website.
Step-by-Step Guide
- Writing Your First Go Program
Go source files end with a .go extension. Create a new file, hello_world.go, and open it in your favorite text editor.
A Go program starts running in a package called main. The fmt package, which stands for format, provides functions for formatted I/O.
Here is how your hello_world.go file should look like:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
- Compiling Your Go Program
Once you've written your program, save the file. Then, open a new terminal window, navigate to the directory where your hello_world.go file is located, and compile it using the go build command:
go build hello_world.go
This will create an executable file in the same directory.
- Running Your Go Program
You can now run your program by typing ./hello_world into the terminal:
./hello_world
Code Examples
Example 1: Hello, World!
Let's break down the Hello, World! program.
// Every Go program starts with a package declaration.
// Programs start running in package main.
package main
// import allows us to use code from other packages.
// The fmt package provides functions for formatted I/O.
import "fmt"
// main is the entry point of our program.
func main() {
// Println function of fmt package prints the text to the console.
fmt.Println("Hello, World!")
}
When you run this program, you will see the text Hello, World! printed to the console.
Summary
Today, you learned how to write, compile, and run a simple Go program. You learned about packages, functions, and how to print text to the console.
Next, you might want to learn more about Go's data types, control structures, or how to write functions. The official Go documentation is a great resource.
Practice Exercises
- Write a program that prints your name to the console.
- Write a program that prints the current date and time.
- Write a program that calculates and prints the result of a simple math operation.
Solutions
- Print Your Name
package main
import "fmt"
func main() {
fmt.Println("Your Name")
}
- Print Current Date and Time
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Now())
}
- Simple Math Operation
package main
import "fmt"
func main() {
fmt.Println("5 + 3 =", 5+3)
}
Try to modify these programs and see what happens. The best way to learn programming is by writing code!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article