go 语言基础

package main
import "fmt"

func main() {

var a int = 10
var b = 20
/// 这种新式声明对象的时候 C 必须为未声明的对象
c := 30

/// 打印
fmt.Println("a =",a,"b =",b,"c =",c)

const num = 100
const num1,num2 = 10,20
fmt.Println(num,num1,num2)

/// iota 表示从0开始,自动加一 声明常量
const(
Kevin = iota + 10
Kevin1
Kevin2
)
fmt.Println(Kevin,Kevin1,Kevin2)

var nums = max(10,20)
/// 打印
fmt.Println(nums)
return_name("kevin")

var array = []int{10,20,30}
/// &取地址
fmt.Println(array,&array[1])

/// 动态数组(切片)
fmt.Println("切片 = ",array[:2])
fmt.Println("切片 = ",array[:])

rangeFunc()

mapFunc()
}

/* 函数返回两个数的最大值 */
func max(num1, num2 int) int {
/* 定义局部变量 */
var result int

if (num1 > num2) {
result = num1
} else {
result = num2
}
return result
}

/// 函数传参
func return_name(kevin string) {

fmt.Println("welcome",kevin)
}

/// Range的使用
func rangeFunc() {

nums := []int{0,22,33,55,66,88}
// for index,num := range nums {
//// 打印索引值和Value
// fmt.Println(index,"=",num)
// }
for num := range nums {
// 打印索引
fmt.Println(num)
}
}

/// map 集合
func mapFunc() {
// var dict map[string]string
dict := make(map[string]string)
dict["kevin"] = "张广义"
dict["Alice"] = "冯殊荣"
dict["Tom"] = "猫"
dict["Jerry"] = "老鼠"

for name := range dict {
fmt.Println(name,"=",dict[name])
}

flagK,flagV := dict["kevin"]
if (flagV) {
fmt.Println(flagK,flagV)
}else {
fmt.Println("No Jack")
}
}  

 

 

package main
import "fmt"

/// 定义接口
type Kevins interface {
calls()
}

/// 定义结构体
type Toms struct {

}

/// 实现接口方法
func (toms Toms) calls(){
fmt.Println("实现接口方法")
}

func main() {

var name Kevins
name = new(Toms)
name.calls()
}

posted @ 2018-11-26 18:10  GYCocoa  阅读(103)  评论(0编辑  收藏  举报