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!
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.
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.
Let's write a simple "Hello, World!" program to get familiar with the Go syntax and the go run command.
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:
go run hello.goš Your program should now output: Hello, World!
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.
In this section, we'll take a quick look at some essential Go concepts: variables, functions, and types.
Variables in Go are defined using the var keyword or implicitly when assigned a value. Here's an example:
name := "John Doe"
age := 30Functions in Go are defined using the func keyword. Here's a simple function that calculates the area of a circle:
func circleArea(radius float64) float64 {
return 3.14 * radius * radius
}Go supports various data types, such as:
boolint8, int16, int32, int64, uint8, uint16, uint32, uint64float32, float64complex64, complex128stringWhat command is used to run a Go program directly?
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! š¤