Home > Article > Backend Development > How to write code in golang
As a relatively young programming language, Golang has received more and more attention and favor, especially in the field of back-end development. This article will introduce the basic syntax and common programming paradigms of Golang to help beginners get started quickly.
1. Basic syntax
There are many ways to declare variables. You can use the var keyword or the := operator. Automatic type deduction:
var i int // 默认值为0 i = 10 j := "Hello World" // 自动推导类型为string
Constant life uses the const keyword, which can be used for enumerated types, values, strings, etc.:
const ( Pi = 3.14159265358979323846 Apples = 1 << iota // Apples的值是1左移0位,即1 Oranges // Oranges的值是1左移1位,即2 Bananas // Bananas的值是1左移2位,即4 )
The control structure of Go language is the same as other languages, including if-else, switch-case, for loop and jump statement:
if i > 10 { fmt.Println("i is greater than 10") } else { fmt.Println("i is less than or equal to 10") } switch { case i < 0: fmt.Println("i is negative") case i > 0: fmt.Println("i is positive") default: fmt.Println("i is zero") } for i := 0; i < 10; i++ { fmt.Println(i) } // 跳转语句 goto label1 fmt.Println("this line will never get executed") label1: fmt.Println("jumped to label1")
Arrays are value types, and the length is determined when declared, while slices are reference types, and the length changes dynamically. Slices can be created through the make function, or can be generated by slicing operations:
// 数组 var arr [5]int for i := 0; i < 5; i++ { arr[i] = i } // 切片的创建和操作 var slice1 []int = arr[1:3] slice2 := make([]int, 5, 10) // 第2个参数是长度,第三个参数是容量 slice2 = append(slice2, 1, 2, 3)
Structures in Golang can not only define fields, but also Define method. The pointer type of the structure can use the . and -> operators to access fields and methods:
type Person struct { name string age int } func (p Person) SayHello() { fmt.Println("Hello, my name is", p.name, ", and I am", p.age, "years old.") } func main() { p := Person{name: "Alice", age: 30} p.SayHello() pp := &p fmt.Println(pp.age) }
2. Programming paradigm
Object-oriented programming in Golang also relies heavily on structures and methods. Here we can use the interface type to define shared methods:
type Animal interface { Speak() string } type Dog struct { name string } func (d Dog) Speak() string { return "Woof! I am " + d.name } func main() { d := Dog{"Fido"} fmt.Println(d.Speak()) }
Golang is a language that naturally supports concurrent programming. Using the two concepts of goroutine and channel, we can easily implement efficient concurrent programs:
func worker(id int, c chan int) { for { n, ok := <-c if !ok { break } fmt.Printf("Worker %d received %d ", id, n) } fmt.Printf("Worker %d is done ", id) } func createWorker(id int) chan<- int { c := make(chan int) go worker(id, c) return c } func main() { c := make(chan int) var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go func(id int) { c := createWorker(id) for j := 0; j < 10; j++ { c <- j } wg.Done() }(i) } wg.Wait() close(c) time.Sleep(time.Second) }
The above is the basic syntax and programming paradigm of Golang. Discussion of more advanced language features and practical application scenarios requires proficiency. After mastering the language proceed to more in-depth study and research.
The above is the detailed content of How to write code in golang. For more information, please follow other related articles on the PHP Chinese website!