Skip to content

Commit

Permalink
First class
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Jul 7, 2021
1 parent af193a2 commit 09fc2e9
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 2 deletions.
10 changes: 10 additions & 0 deletions examples/first_lesson/afterclass/fibonacci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func main() {

}

func fibonacci(n int) int {
// TODO
return 0
}
14 changes: 14 additions & 0 deletions examples/first_lesson/afterclass/fmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func main() {

}

// 输出两位小数
func printNumWith2(float642 float64) string {
return ""
}

func printBytes(data []byte) string {
return ""
}
20 changes: 20 additions & 0 deletions examples/first_lesson/array_slice/array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"

func main() {
// 直接初始化一个三个元素的数组。大括号里面多一个或者少一个都编译不通过
a1 := [3]int{9, 8, 7}
fmt.Printf("a1: %v, len: %d, cap: %d", a1, len(a1), cap(a1))

// 初始化一个三个元素的数组,所有元素都是0
var a2 [3]int
fmt.Printf("a2: %v, len: %d, cap: %d", a2, len(a2), cap(a2))

//a1 = append(a1, 12) 数组不支持 append 操作

// 按下标索引
fmt.Printf("a1[1]: %d", a1[1])
// 超出下标范围,直接崩溃,编译不通过
//fmt.Printf("a1[99]: %d", a1[99])
}
62 changes: 62 additions & 0 deletions examples/first_lesson/array_slice/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import "fmt"

func main() {
s1 := []int{1, 2, 3, 4} // 直接初始化了 4 个元素的切片
fmt.Printf("s1: %v, len %d, cap: %d \n", s1, len(s1), cap(s1))

s2 := make([]int, 3, 4) // 创建了一个包含三个元素,容量为4的切片
fmt.Printf("s2: %v, len %d, cap: %d \n", s2, len(s2), cap(s2))

s2 = append(s2, 7) // 后边添加一个元素,没有超出容量限制,不会发生扩容
fmt.Printf("s2: %v, len %d, cap: %d \n", s2, len(s2), cap(s2))

s2 = append(s2, 8) // 后边添加了一个元素,触发扩容
fmt.Printf("s2: %v, len %d, cap: %d \n", s2, len(s2), cap(s2))

s3 := make([]int, 4) // 只传入一个参数,表示创建一个含有四个元素,容量也为四个元素的
fmt.Printf("s3: %v, len %d, cap: %d \n", s3, len(s3), cap(s3))

// 按下标索引
fmt.Printf("s3[2]: %d", s3[2])
// 超出下标范围,直接崩溃
// runtime error: index out of range [99] with length 4
fmt.Printf("s3[99]: %d", s3[99])

// SubSlice()

//shareArr()
}

func SubSlice() {
s1 := []int{2, 4, 6, 8, 10}
s2 := s1[1:3]
fmt.Printf("s2: %v, len %d, cap: %d \n", s2, len(s2), cap(s2))

s3 := s1[2:]
fmt.Printf("s3: %v, len %d, cap: %d \n", s3, len(s3), cap(s3))

s4 := s1[:3]
fmt.Printf("s4: %v, len %d, cap: %d \n", s4, len(s4), cap(s4))
}

func ShareSlice() {

s1 := []int{1, 2, 3, 4}
s2 := s1[2:]
fmt.Printf("s1: %v, len %d, cap: %d \n", s1, len(s1), cap(s1))
fmt.Printf("s2: %v, len %d, cap: %d \n", s2, len(s2), cap(s2))

s2[0] = 99
fmt.Printf("s1: %v, len %d, cap: %d \n", s1, len(s1), cap(s1))
fmt.Printf("s2: %v, len %d, cap: %d \n", s2, len(s2), cap(s2))

s2 = append(s2, 199)
fmt.Printf("s1: %v, len %d, cap: %d \n", s1, len(s1), cap(s1))
fmt.Printf("s2: %v, len %d, cap: %d \n", s2, len(s2), cap(s2))

s2[1] = 1999
fmt.Printf("s1: %v, len %d, cap: %d \n", s1, len(s1), cap(s1))
fmt.Printf("s2: %v, len %d, cap: %d \n", s2, len(s2), cap(s2))
}
14 changes: 14 additions & 0 deletions examples/first_lesson/fmt/fmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "fmt"

func main() {
name:="Tom"
age := 17
// 这个 API 是返回字符串的,所以大多数时候我们都是用这个
str := fmt.Sprintf("hello, I am %s, I am %d years old \n", name, age)
println(str)

// 这个是直接输出,一般简单程序 DEBUG 会用它输出到一些信息到控制台
fmt.Printf("hello, I am %s, I am %d years old \n", name, age)
}
50 changes: 50 additions & 0 deletions examples/first_lesson/for/for.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import "fmt"

func main() {
ForLoop()
ForI()
ForR()
}

func ForLoop() {
arr := []int {9, 8, 7, 6}
index := 0
for {
if index == 3{
// break 跳出循环
break
}
fmt.Printf("%d => %d ", index, arr[index])
index ++
}
fmt.Println("\n for loop end ")
}

func ForI() {
arr := []int {9, 8, 7, 6}
for i := 0; i < len(arr); i++ {
fmt.Printf("%d => %d", i, arr[i])
}
fmt.Println("\n for i loop end ")
}

func ForR() {
arr := []int {9, 8, 7, 6}
// 如果只是需要 value, 可以用 _ 代替 index
// 如果只需要 index 也可以去掉 写成 for index := range arr
for index, value := range arr {
fmt.Printf("%d => %d", index, value)
}
fmt.Println("for r loop end ")
}









3 changes: 3 additions & 0 deletions examples/first_lesson/func_dec/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ func main() {
b, c := Fun1("a", 17)
println(b)
println(c)

_, d := Fun2("a", "b")
println(d)
}

// Fun0 只有一个返回值,不需要括号括起来
Expand Down
32 changes: 32 additions & 0 deletions examples/first_lesson/if_else/ifelse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import "fmt"

func main() {
Young(9)
Young(100)

IfUsingNewVariable(10, 200)
IfUsingNewVariable(100, 30)
}

func Young(age int) {
if age < 18{
fmt.Println("I am a child!")
} else {
// else 分支也可以没有
fmt.Println("I not a child")
}
}

func IfUsingNewVariable(start int, end int) {
if distance := end - start; distance > 100 {
fmt.Printf("距离太远,不来了: %d\n", distance)
} else {
// else 分支也可以没有
fmt.Printf("距离并不远,来一趟: %d\n", distance)
}

// 这里不能访问 distance
//fmt.Printf("距离是: %d\n", distance)
}
20 changes: 20 additions & 0 deletions examples/first_lesson/switch/switch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"

func main() {
ChooseFruit("蓝莓")
ChooseFruit("苹果")
ChooseFruit("西瓜")
}

func ChooseFruit(fruit string) {
switch fruit {
case "苹果":
fmt.Println("这是一个苹果")
case "草莓", "蓝莓":
fmt.Println("这是霉霉")
default:
fmt.Printf("不知道是啥:%s \n", fruit)
}
}
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package main

func main() {
print("Hello, Go!")
import (
"fmt"
"log"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}

0 comments on commit 09fc2e9

Please sign in to comment.