...
 
Commits (2)
    https://gitcode.net/weixin_46819856/learning_go_tian/-/commit/51fbb85e252d941f4acbeac9351ac917c10a452e the interface is learning 2021-06-03T05:31:17+00:00 Zheng Tian zhengtian@cylonix.cn https://gitcode.net/weixin_46819856/learning_go_tian/-/commit/8412e8c66ce4528636ad7ab8fb38c429834b3cd5 interface.go is Ok 2021-06-03T05:48:48+00:00 Zheng Tian zhengtian@cylonix.cn
......@@ -7,7 +7,7 @@ func change(sliceArr ...string) {
sliceArr = append(sliceArr, "playground")
fmt.Println(sliceArr)
}
func changeArr(arr []int) {
func changeArr(arr []int) {
arr[2] = 222
}
func main() {
......@@ -28,7 +28,7 @@ func main() {
change(welcome...)
fmt.Println(welcome)
arr1 := [3]int{1,2,3}
arr1 := [3]int{1, 2, 3}
changeArr(arr1[:])
fmt.Println(arr1)
}
package main
import "fmt"
type salary_calculator interface {
calculate_salary() int
}
......@@ -23,6 +25,77 @@ func (c contract) calculate_salary() int {
return c.basicpay
}
func total_expens(s []) {
func total_expens(s []salary_calculator) {
expense := 0
for _, v := range s {
expense += v.calculate_salary()
}
fmt.Println(expense)
}
func allType(i interface{}) {
fmt.Printf("Type = %T , value = %v\n", i, i)
}
// assest 类型断言
func assest(i interface{}) {
v, ok := i.(int)
fmt.Println(v, ok)
}
// 类型选择
func findType(i interface{}) {
switch i.(type) {
case string:
fmt.Println("string")
case int:
fmt.Println("int")
case salary_calculator:
fmt.Println("salary_calculator")
default:
fmt.Println("default unknow type")
}
}
// 指针接受者
type describer interface {
}
type address struct {
city, state string
}
func (i *address) show_address() {
fmt.Printf("%v,%v", i.city, i.state)
i.city = "usa"
}
func main() {
a := permanet{
1, 1000, 3000,
}
b := contract{
2, 5000,
}
exployees := []salary_calculator{a, b}
total_expens(exployees)
c := 9
d := ":456"
allType(c)
allType(d)
// 类型断言
assest(c)
assest(d)
// 类型选择
findType(c)
findType(d)
findType(true)
findType(a)
fmt.Println("==================")
AD := address{"china", "1111111.333333"}
p := &AD
p.show_address()
fmt.Println(p, &AD, AD)
}
// interface is ok