Go First Program 🎯

beginner
13 min

Go First Program 🎯

Welcome to your Go journey at CodeYourCraft! In this lesson, we'll write our first Go program and explore the basics of this powerful and popular programming language. By the end, you'll have a solid understanding of how Go works, ready to tackle more complex projects.

What is Go (Golang)? 📝

Go, also known as Golang, is an open-source programming language created by Google in 2009. Go is designed to be easy to learn, efficient, and safe for building simple and large-scale applications.

Why Go? 💡

  • Go is easy to learn, with a syntax that is clear and simple.
  • Go is fast and efficient, making it ideal for building high-performance applications.
  • Go promotes good coding practices, such as concurrency, to help you write maintainable code.

Installing Go 📝

Before we dive into our first program, let's make sure you have Go installed on your computer. Follow the official Go installation guide to install Go on your system.

Your First Go Program 🎯

Now that Go is installed, let's write our first program!

go
package main import "fmt" func main() { fmt.Println("Hello, World!") }

Let's break down the code above:

  1. package main: This line declares that our program belongs to the main package, which is the entry point for every Go program.

  2. import "fmt": We import the fmt package, which contains functions for printing output to the console.

  3. func main(): This function is the main function of our program and is automatically called when we run our program.

  4. fmt.Println("Hello, World!"): This line prints the string "Hello, World!" to the console using the Println function from the fmt package.

Running Your First Go Program 📝

To run the program, save it to a file with a .go extension (e.g., main.go). Open a terminal, navigate to the directory containing your file, and run the following command:

sh
go run main.go

If everything is set up correctly, you should see "Hello, World!" printed to the console. Congratulations! You've written your first Go program.

Next Steps 💡

Now that you've written your first Go program, it's time to explore more. In the next sections, we'll dive deeper into Go concepts, such as variables, functions, and concurrency.


Quick Quiz
Question 1 of 1

What is the purpose of the `package main` line in Go programs?

Quick Quiz
Question 1 of 1

What is the purpose of the `fmt` package in Go programs?