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.

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Covers 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

  1. 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!")
}
  1. 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.

  1. 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

  1. Write a program that prints your name to the console.
  2. Write a program that prints the current date and time.
  3. Write a program that calculates and prints the result of a simple math operation.

Solutions

  1. Print Your Name
package main

import "fmt"

func main() {
    fmt.Println("Your Name")
}
  1. Print Current Date and Time
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now())
}
  1. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help