我爱编程golang notes

golang语言简介

2017-08-16  本文已影响0人  风千寻艾

语言来历

golang是由Google公司在c语言的基础上开发的一款开源的编程语言


环境搭建

golang以及liteIDE安装

  1. 下载完解压
    golang 下载链接 推荐1.7.4版本
  2. 配置环境变量
    配置环境变量 GOROOTGOPATH
  1. 下载完解压
    liteIDE 下载链接 推荐最新版本
  2. 配置
    配置GOPATH、选择编译系统平台且编辑该平台对应的环境(重点关注GOROOT)

特性

// golangtest project main.go
package main

import (
    "fmt"
)

type Base struct {
    FirstName, LastName string
    Age                 float32
}

func (base *Base) HasFeet() {
    fmt.Println(base.FirstName + " " + base.LastName + " has feet! Base")
}

func (base *Base) Flying() {
    fmt.Println("Base Can flying!")
}

type Sub struct {
    Base
    Area string
}

func (sub *Sub) Flying() {
    /* 要调用基类的方法,必须显示调用sub.Base.Flying(),而不是sub.Flying(),否则无限循环调自己 */
    sub.Base.Flying()
    fmt.Println("Sub flying")
}

func main() {
    chk := new(Sub)
    chk.Flying()
    chk2 := &Sub{Base{"Bob", "Steven", 2.0}, "China"}
    fmt.Println(chk2.Area)
    chk2.HasFeet()
}

// golangtest project main.go
package main

import (
    "fmt"
)

type Phone interface {
    call()
}

type NokiaPhone struct {
}

func (nokiaPhone NokiaPhone) call() {
    fmt.Println("I am Nokia, I can call you!")
}

type IPhone struct {
}

func (iPhone IPhone) call() {
    fmt.Println("I am iPhone, I can call you!")
}

func main() {
    var phone Phone

    phone = new(NokiaPhone)
    phone.call()

    phone = new(IPhone)
    phone.call()

}

  go func(){
        fmt.Println("start func")
        time.Sleep(120*time.Second)
    }()                                  

我遇到过的问题

new 的作用是初始化一个指向类型的指针(*T),make 的作用是为 slice,map 或 chan 初始化并返回引用(T)

after they are evaluated, the parameters of the call are passed by value to the
function and the called function begins execution.
请参考


初学golang,欢迎讨论指正


参考:
http://studygolang.com/articles/6365
http://studygolang.com/articles/6382
http://www.runoob.com/go/go-interfaces.html
http://www.01happy.com/golang-diff-between-new-make/
http://kaiq.me/2016/01/02/go/the-parameters-of-the-call-passed-by-value-or-point/

上一篇下一篇

猜你喜欢

热点阅读