Skip to content

Commit

Permalink
更新排便
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 15, 2017
1 parent 959c67f commit 834b949
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 177 deletions.
10 changes: 5 additions & 5 deletions example/array/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import "fmt"
func main() {
// 声明一个长度为5的整数数组
// 一旦数组被声明了,那么它的数据类型跟长度都不能再被改变。
var array1 [5]int
fmt.Printf("array1: %d\n\n", array1)
var array1 [5]int

fmt.Printf("array1: %d\n\n", array1)

// 声明一个长度为5的整数数组
// 初始化每个元素
array2 := [5]int{12, 123, 1234, 12345, 123456}
array2[1] = 5000
fmt.Printf("array2: %d\n\n", array2[1])
fmt.Printf("array2: %d\n\n", array2[1])

// n 是一个长度为 10 的数组
var n [10]int
var i,j int
Expand Down
110 changes: 55 additions & 55 deletions example/byte/byte.go
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)))
}
20 changes: 10 additions & 10 deletions example/const/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import "unsafe"
// 常量可以用len(), cap(), unsafe.Sizeof()常量计算表达式的值。
// 常量表达式中,函数必须是内置函数,否则编译不过:
const (
a = "abc"
b = len(a)
c = unsafe.Sizeof(a)
a = "abc"
b = len(a)
c = unsafe.Sizeof(a)
)

func main(){
const (
PI = 3.14
const1 = "1"
)
const LENGTH int = 10
const e, f, g = 1, false, "str" //多重赋值
println(a, b, c,PI, LENGTH)
const (
PI = 3.14
const1 = "1"
)
const LENGTH int = 10
const e, f, g = 1, false, "str" //多重赋值
println(a, b, c,PI, LENGTH)
}
2 changes: 1 addition & 1 deletion example/float/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func main() {
fmt.Println(x)
fmt.Printf("x is of type %T\n", x)

a := float64(20.0)
a := float64(20.0)
b := 42
fmt.Println(a)
fmt.Println(b)
Expand Down
4 changes: 4 additions & 0 deletions example/func/func.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package main
import "fmt"

type functinTyoe func(int, int)// // 声明了一个函数类型

func (f functinTyoe)Serve() {
fmt.Println("serve2")
}

func serve(int,int) {
fmt.Println("serve1")
}

func main() {
a := functinTyoe(serve)
a(1,2)
Expand Down
27 changes: 14 additions & 13 deletions example/inteface/inteface.go
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))
}

28 changes: 14 additions & 14 deletions example/iota/iota.go
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)
}
5 changes: 2 additions & 3 deletions example/package-demo/cal/add.go
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
}
2 changes: 1 addition & 1 deletion example/package-demo/cal/multi/multiply.go
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
}
5 changes: 2 additions & 3 deletions example/package-demo/cal/subtract.go
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
}
16 changes: 8 additions & 8 deletions example/package-demo/main.go
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)

}
Loading

0 comments on commit 834b949

Please sign in to comment.