package main
import (
"errors"
"fmt"
)
//在go语言中的不可思议的事情返局部变量的指针,一切ok
func myfun() *int {
var a int = 10
return &a
}
//用来处理常规错误
func myerror() {
err := errors.New("emit macho dwarf:elf header corrupted")
if err != nil {
fmt.Print(err)
}
}
//还有就是在go中字符串常量可以修改
func main1() {
var va = float32(3.2)
var myname = "peter"
c := []byte(myname)
c[0] = 'A'
var rawstring = `hello
world`
myname = string(c)
fmt.Println(*myfun(), myname, rawstring, va)
}
const (
i = 1
j = iota
k
)
const v = iota
func main2() {
fmt.Print(i, j, k, v)
}
func main() {
var arr1 [10]int = [10]int{1: 10}
var arr2 [5]int = [5]int{1: 5}
//arr2[:] = arr1[:5]
arr3 := [2]int{1, 2}
arr4 := [...]int{9, 8, 7, 5}
arr5 := [...]int{arr2, 1}
fmt.Print(arr1, "\n", arr2, "\n", arr3, "\n", arr4, "\n", arr5)
}
//go的自定义数据
package main
import (
"fmt"
)
type person struct {
name string
age int
}
func Older(p1, p2 person) (person, int) {
if p1.age > p2.age {
return p1, p1.age - p2.age
} else {
return p2, p2.age - p2.age
}
}
func main() {
var tom person
tom.name, tom.age = "lisi", 200
bob := person{name: "wngwu", age: 23}
//mary := person{"haha",89}
ret, diff := Older(tom, bob)
fmt.Print(ret, diff)
}
//struct匿名字段
package main
import (
"fmt"
)
type person struct {
Human
name string
age int
}
type Human struct {
widgth int
}
func Older(p1, p2 person) (person, int) {
if p1.age > p2.age {
return p1, p1.age - p2.age
} else {
return p2, p2.age - p2.age
}
}
func main() {
var tom person
tom.Human, tom.name, tom.age = Human{89}, "lisi", 200
bob := person{name: "wngwu", age: 223}
bob.Human = Human{899}
ret, diff := Older(tom, bob)
fmt.Print(ret, diff)
}
有疑问加站长微信联系(非本文作者)