Lesson 1
Getting Started with Go: Syntax and Your First Program
Introduction

Hello, future programmers! Are you ready to dive into a new programming adventure? Today, we'll be taking a look at the basics of Go programming — syntax and comments — where you'll write your first, simple Go program.

Go, also known as Golang, is a statically typed, compiled language developed at Google. It's known for its simplicity, efficiency, and ease of use. Notably, Go is widely used for concurrent programming, thanks to its advanced features such as Goroutines and Channels.

Just as English has rules that we call grammar, Go has its own set of regulations known as syntax. Let's get started!

Go Syntax Basics

In Go, we use statements to perform actions. These are usually terminated by a new line or a semicolon (;). The semicolon in the end of the statement is optional and used rarely. Go uses curly braces ({ }) to group statements into blocks.

Take a look at this simple Go syntax example:

Go
1package main 2 3import "fmt" 4 5func main() { 6 fmt.Println("Hello, Go World!") 7}

In this example, package main specifies that it's the main entry point of the Go application. import "fmt" instructs Go to use the fmt library for formatted I/O operations, and in the main function, we print "Hello, Go World!". Though you may not fully understand this code just yet, we will explore each part of it step-by-step in forthcoming lessons.

Say Hello to Comments

Similar to other programming languages, Go supports two kinds of comments: single-line comments and multi-line comments. Single-line comments begin with //, while multi-line comments are surrounded by /* */. Comments provide documentation and critical reference points within the code and don't affect the program execution. They are meant to make the codebase understandable and easy to work with.

Here's an example of how you can use comments in your Go programs:

Go
1package main // Single-line comment 2 3import "fmt" 4 5/* This is a multi-line comment in Go 6 It spans multiple lines 7 Useful for longer descriptions and notes */ 8 9func main() { 10 fmt.Println("Hello, Go World!") // This line prints "Hello, Go World!" 11}
Build Your First Go Program

Now that we've covered the basics, let's delve deeper into the first Go program you'll write! Here is a simple Go program we have already encountered:

Go
1package main 2 3import "fmt" 4 5func main() { 6 fmt.Println("Hello, Go World!") 7}

Let's understand each part of the program:

  • package main: This line declares the name of the package that this file resides in, which in this case is main. The main package is special in Go. It defines an executable program, rather than a library.
  • import "fmt": This statement imports another package into the current package. The package fmt provides functionalities for formatted input/output.
  • func main() { }: This line defines the main function of the program, which is executed when we run our Go program. Similar to Java's public static void main(String[] args), the main function in Go serves as the entry point for the program.
  • fmt.Println("Hello, Go World!"): This is a function call to fmt.Println(), which prints out the passed string to the console.
Lesson Summary and Practice

Congratulations! You've embarked on your Go programming journey and have learned about its syntax and the importance of comments. Upcoming practice exercises will strengthen your understanding and provide hands-on experience in writing your first Go code. Remember, mistakes are stepping stones to success. Let's code away!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.