声明关键字func
package main
import "fmt"
//多返回值
func fun1(a int, b int) (int, int) {
return a, b
}
//无返回值
func fun2(a int) {
fmt.Println(a)
}
//单反回值
func fun3(a string) string {
return a + " test"
}
//可变入参
func fun4(a ...string) {
fmt.Println(len(a))
for i := 0; i < len(a); i++ {
fmt.Println(a[i])
}
for j, v := range a {
fmt.Println(j, v)
}
}
//引用传值、指针 (传入函数类型改为*类型名称)这样就可以在函数中修改参数a的值了
func fun5(a *string) string {
*a = "xxxxxx"
return *a
}
//这里是main方法
func main() {
c, d := fun1(12, 31)
fmt.Println(c)
fmt.Println(d)
_, e := fun1(12, 31) //_代表的值会自动忽略
fmt.Println(e)
fun2(99)
fmt.Println("xxxx")
fun4("a", "b", "c")
//如果已经定义有多个参数,将它们应用到一个可变函数使用func(val…)。
nums := []string{"a", "b", "c"}
fun4(nums...)
a := " q r r "
fmt.Println(a)
fun5(&a) //&a 传入的是参数a 的地址
fmt.Println(a)
//匿名函数
name := func(a, b int) int { return a + b }
fmt.Println(name(1, 2))
}
这种方式我也是第一次接触到,不到对于某些场景,这种方式真心实用。
package main
import "fmt"
//闭包 1
func closures1() func(val int) int {
return func(val int) int {
return val + 10
}
}
//闭包 2
func closures2(valOne int) func(val int) int {
return func(val int) int {
return valOne + val
}
}
//这里是main方法
func main() {
C1 := closures1()
fmt.Println(C1(20))
C2 := closures2(100)
fmt.Println(C2(1))
fmt.Println(C2(2))
}
package main
import "fmt"
func fact(n int) int {
if n == 0 {
return 0
}
return n + fact(n-1)
}
//这里是main方法
func main() {
fmt.Println(fact(100))
}
关键字type ...... struct
package main
import "fmt"
//结构体定义
type person struct {
name string
age int
}
//这里是main方法
func main() {
//结构体声明
var p person
p.name = "xxxx"
p.age = 22
fmt.Println(p)
fmt.Println(p.name)
fmt.Println(person{"xxx", 22})
fmt.Println(person{name: "xxx", age: 22})
fmt.Println(person{name: "xxx"})
fmt.Println(&person{name: "xxx", age: 22})
//结构体声明
s := person{name: "xxx", age: 50}
fmt.Println(s.name)
sp := &s
fmt.Println(sp.age)
sp.age = 60
fmt.Println(sp.age)
}
结构体方法引用
package main
import "fmt"
type rect struct {
width, height int
}
func (r *rect) area() int {
return r.width * r.height
}
func (r rect) perim() int {
return 2*r.width + 2*r.height
}
func main() {
r := rect{width: 10, height: 5}
fmt.Println("area: ", r.area())
fmt.Println("perim:", r.perim())
rp := &r
fmt.Println("area: ", rp.area())
fmt.Println("perim:", rp.perim())
}
声明关键字interface
package main
import "fmt"
type people struct {
name string
age int
}
type people1 struct {
name string
age int
}
//people类型接口实现
func (p people) getname() {
fmt.Println("姓名是:" + p.name)
}
//people类型接口实现
func (p people) getage() {
fmt.Println("年龄是:", p.age)
}
//people类型接口实现
func (p people) getnameone(n string) string {
return p.name + " 和 " + n
}
//people1类型接口实现
func (p1 people1) getname() {
fmt.Println("姓名是:" + p1.name + " 花花")
}
//people1类型接口实现
func (p1 people1) getage() {
fmt.Println("年龄是:", p1.age, " 花花")
}
//people1类型接口实现
func (p1 people1) getnameone(n string) string {
return p1.name + " 和 " + n + " 花花 "
}
//定义接口
type geometry interface {
getname()
getage()
getnameone(n string) string
}
func main() {
r := people{name: "zhangsan", age: 30}
var t geometry
t = r
t.getage()
t.getname()
fmt.Println(t.getnameone("王五"))
r1 := people1{name: "zhangsa1n", age: 60}
t = r1
t.getage()
t.getname()
fmt.Println(t.getnameone("赵六"))
}
package main
import "errors"
import "fmt"
func getval(v int, v1 int) (int, error) {
if v == 0 && v1 == 0 {
return -1, errors.New("不可以都为0")
}
return v + v1, nil
}
func main() {
fmt.Println(getval(0, 0))
}