Welcome to part two of our Go Programming Tutorial Series, designed to build a solid Go (Golang) foundation. This article, focusing on Go Fundamentals: Syntax and Structure, covers everything from your first "Hello, World!" program to variables, constants, data types, and more. Whether you're a novice or seeking to refine your skills, this guide provides the knowledge for writing efficient, clean Go code.
Upon completion, you will be able to:
main
package and main
function.Let's begin!
Every programming journey starts with "Hello, World!". In Go:
<code class="language-go">package main import "fmt" func main() { fmt.Println("Hello, World!") }</code>
package main
: Every Go program begins with a package declaration. main
signifies an executable program.import "fmt"
: Imports the fmt
package for console output (like Println
).func main()
: The program's entry point. Execution starts here.fmt.Println("Hello, World!")
: Prints "Hello, World!" to the console.main
Package and main
Functionmain
package is essential for creating executable Go programs. Without it, your code won't run independently.main
function is mandatory within the main
package; it's the program's starting point.Go is statically typed—you must specify a variable's data type. However, Go also supports type inference for concise code.
Variables are declared using var
:
<code class="language-go">var name string = "Go Programmer" var age int = 30</code>
Shorthand (within functions):
<code class="language-go">name := "Go Programmer" age := 30</code>
Constants are immutable values, declared with const
:
<code class="language-go">const pi float64 = 3.14159</code>
Go offers various built-in data types:
int
, float64
, string
, bool
. Example:<code class="language-go">var age int = 35 var price float64 = 29.99 var name string = "Bob" var isActive bool = true</code>
array
, slice
, struct
, map
. Example:<code class="language-go">// Array var scores [3]int = [3]int{95, 80, 92} // Slice var grades []float64 = []float64{88.2, 91.5, 78.9} // Struct type Person struct { FirstName string LastName string Age int } var person = Person{"Jane", "Doe", 28} // Map var capitals map[string]string = map[string]string{ "France": "Paris", "Italy": "Rome", }</code>
Variables without explicit initialization receive their zero value:
false
for booleans.""
(empty string) for strings.nil
for pointers, slices, maps, and channels.Example:
<code class="language-go">package main import "fmt" func main() { fmt.Println("Hello, World!") }</code>
Go infers variable types from assigned values:
<code class="language-go">var name string = "Go Programmer" var age int = 30</code>
Type conversion requires explicit casting:
<code class="language-go">name := "Go Programmer" age := 30</code>
Let's create a program demonstrating variables, constants, data types, zero values, type inference, and type conversion:
<code class="language-go">const pi float64 = 3.14159</code>
(The code would be identical to the example in the original text, with potentially slightly different variable names to maintain clarity and avoid repetition.)
(The output would be similar to the original example, reflecting the values and types of the variables.)
(The explanation would be the same as in the original, explaining each section of the code.)
<code class="language-go">var age int = 35 var price float64 = 29.99 var name string = "Bob" var isActive bool = true</code>
:=
when the type is obvious.<code class="language-go">// Array var scores [3]int = [3]int{95, 80, 92} // Slice var grades []float64 = []float64{88.2, 91.5, 78.9} // Struct type Person struct { FirstName string LastName string Age int } var person = Person{"Jane", "Doe", 28} // Map var capitals map[string]string = map[string]string{ "France": "Paris", "Italy": "Rome", }</code>
main
Function: Keep main
concise; delegate logic to other functions.This article covered Go's basic syntax and structure, including the "Hello, World!" program, the main
package and function, variables, constants, data types, zero values, type inference, and type conversion. Mastering these fundamentals is crucial for your Go programming journey.
Experiment with the example program or create your own to solidify your understanding.
This article is part of our ongoing Go Tutorial Series. Look for the next tutorial on Control Structures in Go!
Happy coding! ?
The above is the detailed content of Go Basics: Syntax and Structure. For more information, please follow other related articles on the PHP Chinese website!