Introduction to Basic Syntax in Go Programming
1. Hello World
The first program you usually write when learning a new programming language is the "Hello World" program. In Go, it looks like this:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
Explanation:
- The
package main
line tells Go that this is the main package for the program. - The
import "fmt"
line imports the "fmt" package, which contains functions for formatted I/O operations. - The
func main()
line defines the main function, which is the entry point for a Go program. fmt.Println("Hello, World!")
prints "Hello, World!" to the console.
2. Variables
Variables in Go can be declared using the var
keyword or the shorthand :=
syntax.
package main import "fmt" func main() { var name string = "John" age := 30 fmt.Println("Name:", name) fmt.Println("Age:", age) }
Explanation:
var name string = "John"
declares a variable namedname
of typestring
and initializes it with the value "John".age := 30
declares a variable namedage
and initializes it with the value 30. The type ofage
is inferred asint
.
3. Basic Data Types
Go has several basic data types:
int
- integerfloat64
- floating-point numberstring
- sequence of charactersbool
- boolean (true or false)
package main import "fmt" func main() { var a int = 10 var b float64 = 3.14 var c string = "Hello" var d bool = true fmt.Println("Integer:", a) fmt.Println("Float:", b) fmt.Println("String:", c) fmt.Println("Boolean:", d) }
4. Constants
Constants are declared using the const
keyword and cannot be changed after their declaration.
package main import "fmt" func main() { const pi = 3.14159 const greeting = "Hello, Go!" fmt.Println("Pi:", pi) fmt.Println("Greeting:", greeting) }
5. Conditional Statements
Conditional statements in Go are similar to other programming languages. The if
statement is used to execute code based on a condition.
package main import "fmt" func main() { age := 20 if age < 18 { fmt.Println("Minor") } else if age == 18 { fmt.Println("Exactly 18") } else { fmt.Println("Adult") } }
6. Loops
Go supports for
loops, which are used to iterate over a range of values or until a condition is met.
package main import "fmt" func main() { for i := 0; i < 5; i++ { fmt.Println("i =", i) } // Using a while-like loop j := 0 for j < 5 { fmt.Println("j =", j) j++ } }
7. Functions
Functions in Go are defined using the func
keyword. They can take parameters and return values.
package main import "fmt" func add(a int, b int) int { return a + b } func main() { result := add(3, 4) fmt.Println("Result:", result) }
Explanation:
func add(a int, b int) int
defines a function namedadd
that takes two parameters of typeint
and returns anint
.- The
return a + b
statement returns the sum ofa
andb
.
8. Arrays and Slices
Arrays in Go have a fixed size, while slices are dynamically-sized, more flexible views into the elements of an array.
package main import "fmt" func main() { var arr [3]int = [3]int{1, 2, 3} fmt.Println("Array:", arr) slice := []int{4, 5, 6} fmt.Println("Slice:", slice) }
9. Maps
Maps in Go are used to store key-value pairs.
package main import "fmt" func main() { capitals := map[string]string{ "France": "Paris", "Japan": "Tokyo", } fmt.Println("Capital of France:", capitals["France"]) fmt.Println("Capital of Japan:", capitals["Japan"]) }
10. Conclusion
This tutorial introduced the basic syntax of Go programming, covering topics such as variables, data types, constants, conditional statements, loops, functions, arrays, slices, and maps. With these fundamentals, you can start exploring more complex features and libraries in Go.