The most distinctive features of the Go language summarized by ChatGPT

Original link: https://fugary.com/?p=500

The following are some of the most distinctive features of the Go language, with corresponding code examples:

concurrent programming

The Go language natively supports lightweight concurrency, and implements concurrent programming through goroutines and channels.

 package main import ( "fmt" "time" ) func printMessage(message string) { for i := 0; i < 5; i++ { fmt.Println(message) time.Sleep(time.Millisecond * 500) } } func main() { go printMessage("Hello") go printMessage("World") time.Sleep(time.Second * 3) }

garbage collection

The Go language has automatic memory management, using a garbage collector (garbage collector) to automatically release memory that is no longer used.

 package main import "fmt" func createSlice() []int { slice := make([]int, 0, 5) for i := 1; i <= 5; i++ { slice = append(slice, i) } return slice } func main() { slice := createSlice() fmt.Println(slice) }

type inference

The Go language has a type inference function, which can automatically infer the type of a variable based on an assignment statement.

 package main import "fmt" func main() { message := "Hello, Go!" count := 10 ratio := 3.14 fmt.Println(message) fmt.Println(count) fmt.Println(ratio) }

Function with multiple return values

The Go language supports functions returning multiple values, which simplifies code writing in some cases.

 package main import "fmt" func swap(a, b int) (int, int) { return b, a } func main() { x, y := 10, 20 x, y = swap(x, y) fmt.Println(x, y) // 输出:20 10 }

defer statement

The Go language provides the defer statement, which is used to delay the execution of function calls, and is often used for resource release and exception handling.

 package main import "fmt" func cleanup() { fmt.Println("Cleanup") } func main() { defer cleanup() fmt.Println("Hello, Go!") }

Anonymous functions and closures

The Go language supports the concepts of anonymous functions and closures. Functions can be defined inside functions and variables of external functions can be accessed.

 package main import "fmt" func generateIncrementer() func() int { count := 0 return func() int { count++ return count } } func main() { increment := generateIncrementer() fmt.Println(increment()) // 输出:1 fmt.Println(increment()) // 输出:2 fmt.Println(increment()) // 输出:3 }

Interfaces and Type Assertions

The Go language implements polymorphism through interfaces, and provides a type assertion mechanism to check the specific type of the interface at runtime.

 package main import "fmt" type Shape interface { Area() float64 } type Rectangle struct { width float64 height float64 } func (r Rectangle) Area() float64 { return r.width * r.height } type Circle struct { radius float64 } func (c Circle) Area() float64 { return 3.14 * c.radius * c.radius } func printArea(shape Shape) { fmt.Println("Area:", shape.Area()) } func main() { rect := Rectangle{width: 3, height: 4} circ := Circle{radius: 5} printArea(rect) // 输出:Area: 12 printArea(circ) // 输出:Area: 78.5 }

error handling

The Go language handles errors through error value and multiple return values, which simplifies the process of error handling.

 package main import ( "errors" "fmt" ) func divide(a, b float64) (float64, error) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil } func main() { result, err := divide(10, 0) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Result:", result) }

These features make the Go language unique in terms of concurrent programming, memory management, simplicity, and reliability. Please run through these code examples piece by piece to learn more about the coding features of the Go language.

This article is transferred from: https://fugary.com/?p=500
This site is only for collection, and the copyright belongs to the original author.