forked from ZhouYuling/learnGO
-
Notifications
You must be signed in to change notification settings - Fork 0
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
hzchenzhe1
committed
Mar 5, 2018
0 parents
commit 8800280
Showing
36 changed files
with
1,175 additions
and
0 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,47 @@ | ||
##all practice in 《入门GO》 | ||
#predict outcome | ||
1. testmain.go | ||
2. count_characters.go 创建一个用于统计字节和字符(rune)的程序,并对字符串 asSASA ddd dsjkdsjs dk 进行分析,然后再分析 asSASA ddd dsjkdsjsこん dk,最后解释两者不同的原因(提示:使用 unicode/utf8 包 | ||
3. string_conversion2.go 尝试改写 string_conversion2.go 中的代码,要求使用 := 方法来对 err 进行赋值,哪些地方可以被修改? | ||
4. season.go 写一个 Season 函数,要求接受一个代表月份的数字,然后返回所代表月份所在季节的名称(不用考虑月份的日期) | ||
5. for_loop.go 使用 for 结构创建一个简单的循环。要求循环 15 次然后使用 fmt 包来打印计数器的值。 | ||
使用 goto 语句重写循环,要求不能使用 for 关键字。 | ||
6. for_character.go 创建一个程序,要求能够打印类似下面的结果(直到每行 25 个字符时为止):G | ||
GG | ||
GGG | ||
GGGG | ||
GGGGG | ||
GGGGGG | ||
使用 2 层嵌套 for 循环。 | ||
使用一层 for 循环以及字符串截断 | ||
7. bitwise_complement.go 使用按位补码从 0 到 10,使用位表达式 %b 来格式化输出 | ||
8. fizzbuzz.go 写一个从 1 打印到 100 的程序,但是每当遇到 3 的倍数时,不打印相应的数字,但打印一次 "Fizz"。遇到 5 的倍数时,打印 Buzz 而不是相应的数字。对于同时为 3 和 5 的倍数的数,打印 FizzBuzz(提示:使用 switch 语句)。 | ||
9. rectangle_stars.go 使用 * 符号打印宽为 20,高为 10 的矩形 | ||
10. mult_returnval.go 编写一个函数,接收两个整数,然后返回它们的和、积与差。编写两个版本,一个是非命名返回值,一个是命名返回值 | ||
11. error_returnval.go 编写一个名字为 MySqrt 的函数,计算一个 float64 类型浮点数的平方根,如果参数是一个负数的话将返回一个错误。编写两个版本,一个是非命名返回值,一个是命名返回值 | ||
12. varargs.go 写一个函数,该函数接受一个变长参数并对每个元素进行换行打印 | ||
13. 重写本节中生成斐波那契数列的程序并返回两个命名返回值(详见第 6.2 节),即数列中的位置和对应的值,例如 5 与 4,89 与 10 | ||
14. recursive.go 使用递归函数从 10 打印到 1 | ||
15. jiecheng.go 实现一个输出前 30 个整数的阶乘的程序 (使用了 math/big.Int 的Mul) | ||
|
||
|
||
#use case | ||
1. type.go 类型别名 | ||
2. char.go Unicode 字符 | ||
3. presuffix.go 判断string的前缀后缀 | ||
4. index_in_string.go 索引string | ||
5. count_substring.go 统计子字符串 | ||
6. repeat_string.go 重复字符串创建 | ||
7. string_splitjoin.go 字符串split,join | ||
8. string_conversion.go 字符串转其他类型 | ||
9. time.go 时间处理 | ||
10. string_pointer.go 字符串指针 | ||
11. ifelse.go if/else的简单例子 | ||
12. switch.go switch 例子1 | ||
13. switch2.go switch 例子2 | ||
14. for.go for循环例子 | ||
15. for_range.go for-range例子 | ||
16. goto.go goto/label例子 | ||
17. side_effect.go 改变外部变量(函数内部)副作用 | ||
18. defer.go defer例子 | ||
19. package_test.go 测试使用自建package (pack1/pack1.go) |
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,22 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
for i := 0; i <= 10; i++ { | ||
fmt.Printf("the complement of %b is: %b\n", i, ^i) | ||
} | ||
} | ||
/* | ||
the complement of 0 is: -1 | ||
the complement of 1 is: -10 | ||
the complement of 10 is: -11 | ||
the complement of 11 is: -100 | ||
the complement of 100 is: -101 | ||
the complement of 101 is: -110 | ||
the complement of 110 is: -111 | ||
the complement of 111 is: -1000 | ||
the complement of 1000 is: -1001 | ||
the complement of 1001 is: -1010 | ||
the complement of 1010 is: -1011 | ||
*/ |
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() { | ||
var ch int = '\u0041' | ||
var ch2 int = '\u03B2' | ||
var ch3 int = '\U00101234' | ||
fmt.Printf("%d - %d - %d\n", ch, ch2, ch3) | ||
fmt.Printf("%c - %c - %c\n", ch, ch2, ch3) // character | ||
fmt.Printf("%X - %X - %X\n", ch, ch2, ch3) // UTF-8 bytes | ||
fmt.Printf("%U - %U - %U", ch, ch2, ch3) // UTF-8 code point | ||
} | ||
|
||
/* | ||
65 - 946 - 1053236 | ||
A - β - | ||
41 - 3B2 - 101234 | ||
U+0041 - U+03B2 - U+101234 | ||
*/ |
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,21 @@ | ||
package main | ||
|
||
import ( | ||
"unicode/utf8" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
str := "asSASA ddd dsjkdsjs dk" | ||
str1 := "asSASA ddd dsjkdsjsこん dk" | ||
fmt.Printf("str bytes len : %d\n", len(str)) | ||
fmt.Printf("str1 bytes len : %d\n", len(str1)) | ||
fmt.Printf("str character len : %d\n",utf8.RuneCountInString(str)) | ||
fmt.Printf("str1 character len : %d\n",utf8.RuneCountInString(str1)) | ||
} | ||
/* | ||
str bytes len : 22 | ||
str1 bytes len : 28 | ||
str character len : 22 | ||
str1 character len : 24 //日文时3个字节 = 一个字符 | ||
*/ |
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,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
var str string = "Hello, how is it going, Hugo?" | ||
var manyG = "gggggggggg" | ||
|
||
fmt.Printf("Number of H's in %s is: ", str) | ||
fmt.Printf("%d\n", strings.Count(str, "H")) | ||
|
||
fmt.Printf("Number of double g's in %s is: ", manyG) | ||
fmt.Printf("%d\n", strings.Count(manyG, "gg")) | ||
} | ||
|
||
/* | ||
Number of H's in Hello, how is it going, Hugo? is: 2 | ||
Number of double g's in gggggggggg is: 5 | ||
*/ |
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,78 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"io" | ||
) | ||
|
||
func main() { | ||
defera() | ||
deferf() | ||
fmt.Println() | ||
fmt.Println("tracing example:") | ||
defer_b() | ||
fmt.Println() | ||
fmt.Println("lamba defer example:") | ||
func1("Go") | ||
} | ||
//使用 defer 的语句同样可以接受参数,下面这个例子就会在执行 defer 语句时打印 0 | ||
func defera() { | ||
i := 0 | ||
defer fmt.Println(i) | ||
i++ | ||
return | ||
} | ||
//当有多个 defer 行为被注册时,它们会以逆序执行(类似栈,即后进先出): | ||
func deferf() { | ||
for i := 0; i < 5; i++ { | ||
defer fmt.Printf("%d ", i) | ||
} | ||
} | ||
|
||
/* | ||
0 | ||
4 3 2 1 0 | ||
*/ | ||
|
||
//可以用于tracing 函数 | ||
func trace(s string) string { | ||
fmt.Println("entering:", s) | ||
return s | ||
} | ||
|
||
func un(s string) { | ||
fmt.Println("leaving:", s) | ||
} | ||
|
||
func defer_a() { | ||
defer un(trace("a")) | ||
fmt.Println("in a") | ||
} | ||
|
||
func defer_b() { | ||
defer un(trace("b")) | ||
fmt.Println("in b") | ||
defer_a() | ||
} | ||
|
||
/* | ||
tracing example: | ||
entering: b | ||
in b | ||
entering: a | ||
in a | ||
leaving: a | ||
leaving: b | ||
*/ | ||
|
||
//通过lamba函数 | ||
func func1(s string) (n int, err error) { | ||
defer func() { | ||
log.Printf("func1(%q) = %d, %v", s, n, err) | ||
}() | ||
return 7, io.EOF | ||
} | ||
/* | ||
2018/03/05 16:24:55 func1("Go") = 7, EOF | ||
*/ |
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,48 @@ | ||
package main | ||
|
||
import ( | ||
"math" | ||
"fmt" | ||
"errors" | ||
) | ||
|
||
func main() { | ||
ret, err := MySqrt(16) | ||
if err != nil { | ||
fmt.Println("Error! Return values are: ", ret, err) | ||
} else { | ||
fmt.Println("It's ok! Return values are: ", ret, err) | ||
} | ||
if ret2, err2 := MySqrt(5); err2 != nil { | ||
fmt.Println("Error! Return values are: ", ret2, err2) | ||
} else { | ||
fmt.Println("It's ok! Return values are: ", ret2, err2) | ||
} | ||
// named return variables: | ||
fmt.Println(MySqrt2(5)) | ||
} | ||
|
||
func MySqrt(a float64) (float64, error) { | ||
if a < 0 { | ||
return float64(math.NaN()), errors.New("I won't be able to do a sqrt of negative number!") | ||
} | ||
return math.Sqrt(a), nil | ||
} | ||
|
||
func MySqrt2(a float64) (ret float64, err error) { | ||
if a < 0 { | ||
//then you can use those variables in code | ||
ret = float64(math.NaN()) | ||
err = errors.New("I won't be able to do a sqrt of negative number!") | ||
} else { | ||
ret = math.Sqrt(a) | ||
//err is not assigned, so it gets default value nil | ||
} | ||
//automatically return the named return variables ret and err | ||
return | ||
} | ||
/** | ||
It's ok! Return values are: 4 <nil> | ||
It's ok! Return values are: 2.23606797749979 <nil> | ||
2.23606797749979 <nil> | ||
*/ |
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,37 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
result := 0 | ||
idx := 0 | ||
for i := 0; i <= 10; i++ { | ||
result, idx = fibonacci(i) | ||
fmt.Printf("fibonacci(%d) is: %d, idx: %d \n", i, result, idx) | ||
} | ||
} | ||
|
||
func fibonacci(n int) (res int, idx int) { | ||
if n <= 1 { | ||
res = 1 | ||
} else { | ||
res1, _ := fibonacci(n-1) | ||
res2, _ := fibonacci(n-2) | ||
res = res1 + res2 | ||
} | ||
idx = n | ||
return | ||
} | ||
/* | ||
fibonacci(0) is: 1, idx: 0 | ||
fibonacci(1) is: 1, idx: 1 | ||
fibonacci(2) is: 2, idx: 2 | ||
fibonacci(3) is: 3, idx: 3 | ||
fibonacci(4) is: 5, idx: 4 | ||
fibonacci(5) is: 8, idx: 5 | ||
fibonacci(6) is: 13, idx: 6 | ||
fibonacci(7) is: 21, idx: 7 | ||
fibonacci(8) is: 34, idx: 8 | ||
fibonacci(9) is: 55, idx: 9 | ||
fibonacci(10) is: 89, idx: 10 | ||
*/ |
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,24 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
const ( | ||
FIZZ = 3 | ||
BUZZ = 5 | ||
FIZZBUZZ = 15 | ||
) | ||
|
||
func main() { | ||
for i := 0; i <= 100; i++ { | ||
switch { | ||
case i%FIZZBUZZ == 0: | ||
fmt.Println("FizzBuzz") | ||
case i%FIZZ == 0: | ||
fmt.Println("Fizz") | ||
case i%BUZZ == 0: | ||
fmt.Println("Buzz") | ||
default: | ||
fmt.Println(i) | ||
} | ||
} | ||
} |
Oops, something went wrong.