-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ") | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |