Go run Command šŸš€

beginner
23 min

Go run Command šŸš€

Welcome to our comprehensive guide on the go run command! By the end of this lesson, you'll be able to compile and run Go (Golang) programs with ease. Let's dive in!

What is the go run command? šŸ’”

The go run command is a built-in Go tool that allows you to execute Go programs directly, without the need for a separate compile and run step. It simplifies the development process by combining both steps into one.

Setting up your Go environment šŸ“

Before we get started, make sure you have Go installed on your machine. If you haven't installed it yet, you can download it from official Go website.

Writing your first Go program šŸŽÆ

Let's write a simple "Hello, World!" program to get familiar with the Go syntax and the go run command.

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

Save this code in a file named hello.go. To run this program, open your terminal and navigate to the directory containing hello.go, then execute the following command:

bash
go run hello.go

šŸŽ‰ Your program should now output: Hello, World!

Understanding the Go file structure šŸ“

Every Go program consists of one or more .go files. The main file is usually named main.go and contains the main() function, which serves as the entry point of the program.

Variables, Functions, and Types in Go šŸ’”

In this section, we'll take a quick look at some essential Go concepts: variables, functions, and types.

Variables šŸ“

Variables in Go are defined using the var keyword or implicitly when assigned a value. Here's an example:

go
name := "John Doe" age := 30

Functions šŸ“

Functions in Go are defined using the func keyword. Here's a simple function that calculates the area of a circle:

go
func circleArea(radius float64) float64 { return 3.14 * radius * radius }

Types šŸ“

Go supports various data types, such as:

  • Boolean: bool
  • Integer: int8, int16, int32, int64, uint8, uint16, uint32, uint64
  • Floating-point: float32, float64
  • Complex: complex64, complex128
  • String: string

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What command is used to run a Go program directly?

Next Steps šŸš€

Now that you have a solid understanding of the go run command and some fundamental Go concepts, you're ready to dive deeper into the language. Stay tuned for our upcoming lessons on Go data structures, control structures, packages, and more!

Happy coding! 🤘