-
-
Notifications
You must be signed in to change notification settings - Fork 163
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
1 parent
959c67f
commit 834b949
Showing
13 changed files
with
180 additions
and
177 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
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,60 +1,60 @@ | ||
package main | ||
import ( | ||
"bytes" | ||
"fmt" | ||
"bytes" | ||
"fmt" | ||
) | ||
func main() { | ||
// 这里不能写成 b := []byte{"Golang"},这里是利用类型转换。 | ||
b := []byte("Golang") | ||
subslice1 := []byte("go") | ||
subslice2 := []byte("Go") | ||
// func Contains(b, subslice [] byte) bool | ||
// 检查字节切片b ,是否包含子字节切片 subslice | ||
fmt.Println(bytes.Contains(b, subslice1)) | ||
fmt.Println(bytes.Contains(b, subslice2)) | ||
|
||
|
||
s2 := []byte("同学们,上午好") | ||
m := func(r rune) rune { | ||
if r == '上' { | ||
r = '下' | ||
} | ||
return r | ||
} | ||
fmt.Println(string(s2)) | ||
// func Map(mapping func(r rune) rune, s []byte) []byte | ||
// Map函数: 首先将 s 转化为 UTF-8编码的字符序列, | ||
// 然后使用 mapping 将每个Unicode字符映射为对应的字符, | ||
// 最后将结果保存在一个新的字节切片中。 | ||
fmt.Println(string(bytes.Map(m, s2))) | ||
|
||
|
||
s3 := []byte("google") | ||
old := []byte("o") | ||
//这里 new 是一个字节切片,不是关键字了 | ||
new := []byte("oo") | ||
n := 1 | ||
// func Replace(s, old, new []byte, n int) []byte | ||
//返回字节切片 S 的一个副本, 并且将前n个不重叠的子切片 old 替换为 new,如果n < 0 那么不限制替换的数量 | ||
fmt.Println(string(bytes.Replace(s3, old, new, n))) | ||
fmt.Println(string(bytes.Replace(s3, old, new, -1))) | ||
|
||
|
||
// 将字节切片 转化为对应的 UTF-8编码的字节序列,并且返回对应的 Unicode 切片。 | ||
s4 := []byte("中华人民共和国") | ||
r1 := bytes.Runes(s4) | ||
// func Runes(b []byte) []rune | ||
fmt.Println(string(s4), len(s4)) // 字节切片的长度 | ||
fmt.Println(string(r1), len(r1)) // rune 切片的长度 | ||
|
||
|
||
// 字节切片 的每个元素,依旧是字节切片。 | ||
s5 := [][]byte{ | ||
[]byte("你好"), | ||
[]byte("世界"), //这里的逗号,必不可少 | ||
} | ||
sep := []byte(",") | ||
// func Join(s [][]byte, sep []byte) []byte | ||
// 用字节切片 sep 吧 s中的每个字节切片连接成一个,并且返回. | ||
fmt.Println(string(bytes.Join(s5, sep))) | ||
// 这里不能写成 b := []byte{"Golang"},这里是利用类型转换。 | ||
b := []byte("Golang") | ||
subslice1 := []byte("go") | ||
subslice2 := []byte("Go") | ||
// func Contains(b, subslice [] byte) bool | ||
// 检查字节切片b ,是否包含子字节切片 subslice | ||
fmt.Println(bytes.Contains(b, subslice1)) | ||
fmt.Println(bytes.Contains(b, subslice2)) | ||
|
||
|
||
s2 := []byte("同学们,上午好") | ||
m := func(r rune) rune { | ||
if r == '上' { | ||
r = '下' | ||
} | ||
return r | ||
} | ||
fmt.Println(string(s2)) | ||
// func Map(mapping func(r rune) rune, s []byte) []byte | ||
// Map函数: 首先将 s 转化为 UTF-8编码的字符序列, | ||
// 然后使用 mapping 将每个Unicode字符映射为对应的字符, | ||
// 最后将结果保存在一个新的字节切片中。 | ||
fmt.Println(string(bytes.Map(m, s2))) | ||
|
||
|
||
s3 := []byte("google") | ||
old := []byte("o") | ||
//这里 new 是一个字节切片,不是关键字了 | ||
new := []byte("oo") | ||
n := 1 | ||
// func Replace(s, old, new []byte, n int) []byte | ||
//返回字节切片 S 的一个副本, 并且将前n个不重叠的子切片 old 替换为 new,如果n < 0 那么不限制替换的数量 | ||
fmt.Println(string(bytes.Replace(s3, old, new, n))) | ||
fmt.Println(string(bytes.Replace(s3, old, new, -1))) | ||
|
||
|
||
// 将字节切片 转化为对应的 UTF-8编码的字节序列,并且返回对应的 Unicode 切片。 | ||
s4 := []byte("中华人民共和国") | ||
r1 := bytes.Runes(s4) | ||
// func Runes(b []byte) []rune | ||
fmt.Println(string(s4), len(s4)) // 字节切片的长度 | ||
fmt.Println(string(r1), len(r1)) // rune 切片的长度 | ||
|
||
|
||
// 字节切片 的每个元素,依旧是字节切片。 | ||
s5 := [][]byte{ | ||
[]byte("你好"), | ||
[]byte("世界"), //这里的逗号,必不可少 | ||
} | ||
sep := []byte(",") | ||
// func Join(s [][]byte, sep []byte) []byte | ||
// 用字节切片 sep 吧 s中的每个字节切片连接成一个,并且返回. | ||
fmt.Println(string(bytes.Join(s5, sep))) | ||
} |
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
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 |
---|---|---|
@@ -1,43 +1,44 @@ | ||
package main | ||
import ( | ||
"fmt" | ||
"math" | ||
"fmt" | ||
"math" | ||
) | ||
|
||
/* 定义一个 interface */ | ||
type shape interface { | ||
area() float64 | ||
area() float64 | ||
} | ||
|
||
/* 定义一个 circle */ | ||
type circle struct { | ||
x,y,radius float64 | ||
x,y,radius float64 | ||
} | ||
|
||
/* 定义一个 rectangle */ | ||
type rectangle struct { | ||
width, height float64 | ||
width, height float64 | ||
} | ||
|
||
/* 定义一个circle方法 (实现 shape.area())*/ | ||
func(circle circle) area() float64 { | ||
return math.Pi * circle.radius * circle.radius | ||
return math.Pi * circle.radius * circle.radius | ||
} | ||
|
||
/* 定义一个rectangle方法 (实现 shape.area())*/ | ||
func(rect rectangle) area() float64 { | ||
return rect.width * rect.height | ||
return rect.width * rect.height | ||
} | ||
|
||
/* 定义一个shape的方法*/ | ||
func getArea(shape shape) float64 { | ||
return shape.area() | ||
return shape.area() | ||
} | ||
|
||
func main() { | ||
circle := circle{x:0,y:0,radius:5} | ||
rectangle := rectangle {width:10, height:5} | ||
circle := circle{x:0,y:0,radius:5} | ||
rectangle := rectangle {width:10, height:5} | ||
|
||
fmt.Printf("circle area: %f\n",getArea(circle)) | ||
fmt.Printf("rectangle area: %f\n",getArea(rectangle)) | ||
} | ||
fmt.Printf("circle area: %f\n",getArea(circle)) | ||
fmt.Printf("rectangle area: %f\n",getArea(rectangle)) | ||
} | ||
|
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,18 +1,18 @@ | ||
package main | ||
import "fmt" | ||
func main() { | ||
const ( | ||
// 第一个 iota 等于 0,每当 iota 在新的一行被使用时,它的值都会自动加 1; | ||
// 所以 a=0, b=1, c=2 可以简写为如下形式: | ||
a = iota //0 | ||
b //1 | ||
c //2 | ||
d = "ha" //独立值,iota += 1 | ||
e //"ha" iota += 1 | ||
f = 100 //iota +=1 | ||
g //100 iota +=1 | ||
h = iota //7,恢复计数 | ||
i //8 | ||
) | ||
fmt.Println(a,b,c,d,e,f,g,h,i) | ||
const ( | ||
// 第一个 iota 等于 0,每当 iota 在新的一行被使用时,它的值都会自动加 1; | ||
// 所以 a=0, b=1, c=2 可以简写为如下形式: | ||
a = iota //0 | ||
b //1 | ||
c //2 | ||
d = "ha" //独立值,iota += 1 | ||
e //"ha" iota += 1 | ||
f = 100 //iota +=1 | ||
g //100 iota +=1 | ||
h = iota //7,恢复计数 | ||
i //8 | ||
) | ||
fmt.Println(a,b,c,d,e,f,g,h,i) | ||
} |
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,6 +1,5 @@ | ||
package cal | ||
|
||
func Add(num1 int,num2 int) (result int) { | ||
return num1 + num2 | ||
} | ||
|
||
return num1 + num2 | ||
} |
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,5 @@ | ||
package multi | ||
|
||
func Multi(num1 int, num2 int) (result int) { | ||
return num1 * num2 | ||
return num1 * num2 | ||
} |
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,6 +1,5 @@ | ||
package cal | ||
|
||
func Subtract(num1 int,num2 int) (result int) { | ||
return num1 + num2 | ||
} | ||
|
||
return num1 + num2 | ||
} |
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,20 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"package-demo/cal/multi" | ||
"package-demo/cal" | ||
"fmt" | ||
"package-demo/cal/multi" | ||
"package-demo/cal" | ||
) | ||
|
||
func main() { | ||
result := multi.Multi(1,2) | ||
fmt.Printf("%d\n",result) | ||
|
||
result := multi.Multi(1,2) | ||
fmt.Printf("%d\n",result) | ||
|
||
result2 := cal.Add(1,2) | ||
fmt.Printf("%d\n",result2) | ||
|
||
result3 := cal.Subtract(1,2) | ||
fmt.Printf("%d\n",result3) | ||
result3 := cal.Subtract(1,2) | ||
fmt.Printf("%d\n",result3) | ||
|
||
} |
Oops, something went wrong.