Go is a statically typed, compiled programming language designed for simplicity, efficiency, and concurrency. Developed by Google, it aims to make software development straightforward and scalable.
“Ensure that Go is properly installed on your system. If it is not installed, you can refer to Chapter 1 to installing . You can verify the installation by opening a terminal or command prompt and typing:”
go version
If installed correctly, it will display the installed Go version.
Create a directory for your Go projects. Inside this directory, you’ll typically have three folder
src
: Contains your Go source code files.pkg
: Stores package objects.bin
: Keeps executable binaries generated from your Go code.Let’s revisit the “Hello, World!” example and break it down further:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
package main
: Every Go file belongs to a package. The main
package is the entry point for execution.import "fmt"
: Imports the fmt
package, which provides functions for formatting input and output.func main() { ... }
: Defines the main
function, which serves as the entry point of the program.fmt.Println("Hello, World!")
: Calls the Println
function from the fmt
package to print “Hello, World!” to the console.
package main
import "fmt"
func main() {
// Define variables
var num1, num2 int
// Prompt user to enter two numbers
fmt.Println("Enter the first number:")
fmt.Scanln(&num1)
fmt.Println("Enter the second number:")
fmt.Scanln(&num2)
// Calculate the sum
sum := num1 + num2
// Print the result
fmt.Println("The sum is:", sum)
}
fmt
package to use its functions for input and output.main()
function is the entry point of the program.num1
and num2
to store the user input.fmt.Scanln()
function, which reads user input from the console.num1
and num2
and store it in the variable sum
.fmt.Println()
.In this chapter, we've explored the basics of Go programming, including installation, setting up your workspace, and writing your first program With this foundation, you're equipped to tackle more advanced topics in Go programming.Stay tuned for upcoming chapters where we'll delve deeper into Go's features and functionalities. Keep practicing and experimenting with code to reinforce your understanding. Happy coding !❤️