Go学习笔记

package main

import (
    "fmt"
    "github.com/go-errors/errors"
    "log"
    "net/http"
    "os"
    "runtime"
    "time"
    "../webtest"
)

type Saiyan struct {
    Name string
    Power int
}

/*goku := Saiyan{
    Name: "Goku",
    Power: 9000,
}*/

/*a := [3]{1,2,3}
b := [10]{1,2,3,4}
c := [...]int{1,2,3,45}*/

//函数的声明  开头字母大写可以导出 在返回值得表示中 尽量命名返回值 虽然可以不这样做
func SumAndProduct(A,B int)(int,int){
    return A+B,A*B
}

//参数x的值只是一份拷贝 并不会修改x原来的值
func add1(a int)int {
    a = a+1
    return a
}

//只有add2函数知道变量x所在的地址 才能修改变量x的值
//所以我们需要将x所在的地址&x传入函数 并将函数的参数的类型由int修改为*int
//即修改为指针类型 才能在函数中修改x变量的值
func add2(a *int)int {
    *a = *a + 1
    return *a
}

func ReadWrite() bool{
    //file.Open("file")
    return true
}

type Human struct {
    name string
    age int
    phone string
}

type Student struct{
    Human
    school string
    loan float32
}

type Employee struct {
    Human
    company string
    money float32
}

//human对象实现SayHi方法
func(h *Human) SayHi(){
    fmt.Printf("Hi,I am %s you can call me on %s\n",h.name,h.phone)
}

//human对象实现Sing方法
func (h *Human) Sing(lyrics string){
    fmt.Println("La la,la la la",lyrics)
}

func (h *Human) Guzzle(beerStein string){
    fmt.Println("Guzze Guzz..",beerStein)
}

//Employee重载human的Sayhi方法
func(e *Employee) SayHi(){
    fmt.Printf("Hi,I am %s,I work at %s.Call me on %s\n",e.name,e.company,e.phone)
}

func (s *Student) BorrowMoney(amount float32){
    s.loan += amount
}

func (e *Employee) SpendSalary(amount float32){
    e.money -= amount
}

//定义interface
//如果一个对象实现了某个接口的所有方法 则此对象就实现了此接口
type Men interface {
    SayHi()
    Sing(lyrics string)
    Guzzle(beerStein string)
}

type YoungChap interface {
    SayHi()
    Sing(song string)
    BorrowMoney(amount float32)
}

type ElderlyGent interface {
    SayHi()
    Sing(song string)
    SpendSalary(amount float32)
}


type Men2 interface{
    SayHi()
    Sing(lyrics string)
}

func say(s string) {
    for i := 0; i < 5; i++ {
        runtime.Gosched()//让cpu将时间让给别人 下次某个时候继续恢复执行
        fmt.Println(s)
    }
}

/*ci := make(chan int)
cs := make(chan string)
cf := make(chan interface{})*/

func sum(a[] int,c chan int){
    sum := 0
    for _,v := range a{
        sum += v
    }
    c <- sum //send sum to c
}

func fibonacci(n int,c chan int){
    x,y := 1,1
    for i:=0;i

你可能感兴趣的:(Go学习笔记)