Golang入门必备:掌握这十五大核心概念!
发表时间: 2024-07-31 20:28
想成为一名Golang开发大神?这15个核心概念你必须掌握!每个概念都附带代码示例,学完你就离大牛不远了!
package mainimport "fmt"func main() { var a int = 10 b := 20 var c, d, e int = 1, 2, 3 var f *int = &a fmt.Println(a, b, c, d, e, f, *f)}
package mainimport "fmt"func main() { const pi float64 = 3.14159 const e = 2.71828 const ( Sunday = iota Monday Tuesday Wednesday Thursday Friday Saturday ) fmt.Println(pi, e, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)}
package mainimport "fmt"func main() { var a int = 10 var b float64 = 3.14 var c bool = true var d string = "hello" var arr [3]int = [3]int{1, 2, 3} slice := []int{4, 5, 6} m := map[string]int{"foo": 1, "bar": 2} type Person struct { Name string Age int } person := Person{"Alice", 30} fmt.Println(a, b, c, d, arr, slice, m, person)}
package mainimport "fmt"func main() { if x := 10; x < 20 { fmt.Println("x is less than 20") } for i := 0; i < 5; i++ { fmt.Println(i) } switch day := 3; day { case 1: fmt.Println("Monday") case 2: fmt.Println("Tuesday") default: fmt.Println("Other day") }}
package mainimport "fmt"func add(a int, b int) int { return a + b}func swap(x, y string) (string, string) { return y, x}func main() { result := add(1, 2) fmt.Println(result) a, b := swap("hello", "world") fmt.Println(a, b) anon := func(x int) int { return x * x } fmt.Println(anon(5))}
package mainimport "fmt"func main() { var arr [3]int = [3]int{1, 2, 3} slice := []int{4, 5, 6} slice = append(slice, 7, 8) newSlice := slice[1:3] fmt.Println(arr, slice, newSlice)}
package mainimport "fmt"func main() { m := map[string]int{"foo": 1, "bar": 2} m["baz"] = 3 delete(m, "bar") fmt.Println(m)}
package mainimport "fmt"type Person struct { Name string Age int}func (p Person) Greet() { fmt.Println("Hello, my name is", p.Name)}func main() { person := Person{"Alice", 30} person.Greet()}
package mainimport "fmt"type Speaker interface { Speak() string}type Dog struct{}func (d Dog) Speak() string { return "Woof!"}func main() { var s Speaker = Dog{} fmt.Println(s.Speak())}
package mainimport "fmt"func main() { var x int = 10 var p *int = &x fmt.Println(p, *p)}
package mainimport ( "fmt" "time")func say(s string) { for i := 0; i < 5; i++ { fmt.Println(s) time.Sleep(100 * time.Millisecond) }}func main() { go say("world") say("hello")}
package mainimport ( "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(4, 0) if err != nil { fmt.Println("Error:", err) } else { fmt.Println("Result:", result) }}
package mainimport ( "fmt" "time")func main() { fmt.Println("Current time:", time.Now())}
package mainimport ( "github.com/gin-gonic/gin")func main() { r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) }) r.Run()}
package mainimport ( "fmt" "reflect")func main() { var x float64 = 3.4 fmt.Println("type:", reflect.TypeOf(x)) fmt.Println("value:", reflect.ValueOf(x))}
以上就是Golang的15个核心概念,每个概念都附带了代码示例。掌握这些内容,你将从Golang小白进阶为Golang开发大神!赶快动手练习吧!