Tianhe Gao

First time using Golang

Golang Wikipedia

1package main
2
3import "fmt"
4
5func main() {
6  fmt.Println("Hello World!")
7}
 1// Concurrency
 2package main
 3
 4import (
 5  "fmt"
 6  "time"
 7)
 8
 9func readword(ch chan string) {
10  fmt.Println("Type a word, then hit Enter.")
11  var word string
12  fmt.Scanf("%s", &word)
13  ch <- word
14}
15
16func timeout(t chan bool) {
17  time.Sleep(5 * time.Second)
18  t <- false
19}
20
21func main() {
22  t := make(chan bool)
23  go timeout(t)
24  
25  ch := make(chan string)
26  go readword(ch)
27  
28  select {
29  case word := <-ch:
30    fmt.Println("Received", word)
31  case <-t:
32    fmt.Println("Timeout.")
33  }
34}
 1// testing
 2func ExtractUsername(email string) string {
 3  at := strings.Index(email, "@")
 4  return email[:at]
 5}
 6
 7//----
 8
 9import (
10  "testing"
11)
12
13func TestExtractUsername(t *testing.T) {
14  t.Run("withoutDot", func(t *testing.T) {
15    username := ExtractUsername("r@google.com")
16    if username != "r" {
17      t.Fatalf("Got: %v\n", username)
18    }
19  })
20  
21  t.Run("withDot", func(t *testing.T) {
22    username := ExtractUsername("john.smith@example.com")
23    if username != "john.smith" {
24      t.Fatalf("Got: %v\n", username)
25    }
26  })
27}
 1// web app
 2package main
 3
 4import (
 5  "fmt"
 6  "log"
 7  "net/http"
 8)
 9
10func helloFunc(w http.ResponseWriter, r *http.Request) {
11  fmt.Fprintf(w, "Hello World!")
12}
13
14func main() {
15  http.HandleFunc("/", helloFunc)
16  log.Fatal(http.ListenAndServe(":8080", nil))
17}

A Tour of Go

https://go.dev/tour/list

  • packages, variables, functions
  • flow control statements: for, if, else, switch and defer
  • structs, slices, and maps
  • methods, interfaces
  • generics
  • concurrency
 1// packages.go
 2package main
 3
 4import (
 5    "fmt"
 6    "math/rand"
 7)
 8
 9func main() {
10    fmt.Println("My favorite number is", rand.Intn(10))
11}
 1// imports.go
 2package main
 3
 4import (
 5    "fmt"
 6    "math"
 7)
 8
 9func main() {
10    fmt.Println("Now you have %g problems.\n", math.Sqrt(7))
11}
 1// exported-names.go
 2package main
 3
 4import (
 5    "fmt"
 6    "math"
 7)
 8
 9func main() {
10    fmt.Println(math.Pi)
11}
 1// functions.go
 2package main
 3
 4import "fmt"
 5
 6func add(x, y int) int {
 7    return x + y
 8}
 9
10func main() {
11    fmt.Println(add(42, 13))
12}
 1// multiple-results.go
 2package main
 3
 4import "fmt"
 5
 6func swap(x, y, z string) (string, string, string) {
 7    return y, x, z
 8}
 9
10func main() {
11    a, b, c := swap("Hello", "World", "!")
12    fmt.Println(a,b,c)
  1. 再找一个官方教程

refer

作为一位对Go语言一点也不了解的人,如何入门Go语言

如果您是Go语言的初学者,以下是一些建议来帮助您入门:

了解Go语言的特点:Go语言因其简洁、高效、并发处理能力强等特点而受到关注。了解这些特点可以帮助您理解为什么Go语言在开发中受到青睐。

安装Go语言环境:前往Go语言的官方网站下载并安装Go语言环境。这是一个简单的过程,只需按照指示操作即可。

学习基本语法:开始学习Go语言的基本语法,包括变量声明、数据类型、控制结构(if、for、switch等)、函数定义等。您可以通过在线教程、书籍或官方文档来学习。

动手实践:理论知识需要通过实践来巩固。尝试编写一些简单的程序,如“Hello World”、计算器等,来熟悉Go语言的编程范式。

理解并发编程:Go语言的并发模型是其一大特色,通过Goroutines和Channels可以轻松实现并发编程。理解这些概念对于掌握Go语言至关重要。

学习标准库:Go语言有一个强大的标准库,涵盖了网络编程、文件操作、加密等多种功能。熟悉这些库可以大大提高开发效率。

参与社区:加入Go语言的社区,如Go语言中国、Stack Overflow上的Go标签等。在这些社区中,您可以提问、回答问题、分享经验,与其他Go开发者交流。

项目实战:通过实际项目来应用Go语言。可以从一些简单的Web应用开始,逐步尝试更复杂的项目。

持续学习:Go语言仍在快速发展中,持续关注Go语言的最新动态,学习新的特性和最佳实践。

通过以上步骤,您可以逐步建立起对Go语言的了解,并能够在实践中不断提升技能。记住,学习编程语言需要时间和耐心,不断实践和积累经验是提高编程技能的关键。


https://commandcenter.blogspot.com/2024/01/what-we-got-right-what-we-got-wrong.html

https://geektutu.com/post/quick-golang.html

https://geektutu.com/post/gee.html


No notes link to this note