package main
import (
"fmt"
)
var (
aa int
bb bool
)
func main() {
fmt.Println("Hello World!")
var i int
i = 10
var j = "jjjjjjjjjjjj"
k := "kkkkkkkkkkk"
fmt.Println("变量:", i)
fmt.Println("变量:", j)
fmt.Println("变量:", k)
var s, u, g int
s, u, g = 10, 20, 30
fmt.Println("变量:", s)
fmt.Println("变量:", u)
fmt.Println("变量:", g)
var a, b, c = "aaa", "bbb", "ccc"
fmt.Println("变量:", a)
fmt.Println("变量:", b)
fmt.Println("变量:", c)
d, e, f := "ddd", "eee", "fff"
fmt.Println("变量:", d)
fmt.Println("变量:", e)
fmt.Println("变量:", f)
fmt.Println("全局变量:", aa)
fmt.Println("全局变量:", bb)
fmt.Println("内存地址:", &a)
const LEFT int = 100
const RIGHT = 200
const UP, DOWN = 300, 400
const (
BEFORE = 500
AFTER = 600
)
fmt.Println("常量:", LEFT, RIGHT, UP, DOWN, BEFORE, AFTER)
var v, z int = 10, 20
fmt.Println("加法:", v+z)
fmt.Println("减法:", v-z)
fmt.Println("乘法:", v*z)
fmt.Println("除法:", v/z)
fmt.Println("取余:", v%z)
fmt.Println("小于等于:", v <= z)
fmt.Println("等于:", v == z)
if 10 == 10 {
fmt.Println("if语句")
}
if 10 == 20 {
fmt.Println("if语句")
} else {
fmt.Println("else语句")
}
jk := "a"
switch jk {
case "a":
fmt.Println("分支语句")
break
case "b":
fmt.Println("xxxxx")
break
default:
fmt.Println("--------")
break
}
var index = 1
for index < 4 {
fmt.Println("-------")
index++
}
var flat int = 3
A:
index = 1
for index < 3 {
fmt.Println("xxxx")
index++
}
flat++
if flat < 5 {
goto A
}
testFun1()
testFun2(10, 20)
fmt.Println("函数返回:", testFun3(10, 20))
x, y := testFun4(10, 20)
fmt.Println("函数返回多个值:", x, y)
var arr [3]int
arr[0] = 1
arr[1] = 10
arr[2] = 32
{
var op int = 0
for op < len(arr) {
fmt.Println(arr[op])
op++
}
}
var arr1 = [...]float64{12.12, 34.2, 45.2}
fmt.Println(arr1)
var arr2 = [5]float32{11.23, 343.23, 34.5}
fmt.Println(arr2)
var in int = 100
fmt.Println("变量的地址", &in)
var ini *int
ini = &in
fmt.Println("指针获取数据", *ini)
var ink *int
fmt.Println("判断空指针", (ink == nil))
type Book struct {
title string
num int
names []float32
flag bool
}
var book1 Book
book1.flag = true
book1.num = 10
book1.title = "曹雪坤"
book1.names = []float32{1000.0, 2.0, 3.4, 7.0, 50.0}
fmt.Println(book1)
numbers := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
fmt.Println(numbers)
fmt.Println("numbers[1:4] ==", numbers[1:4])
fmt.Println("numbers[:3] ==", numbers[:3])
fmt.Println("numbers[4:] ==", numbers[4:])
fmt.Println("计算切片的容量:", cap(numbers))
fmt.Println("计算切片的长度:", len(numbers))
numbers = append(numbers, 13)
fmt.Println("追加元素后:", numbers)
numbers = append(numbers[:9], numbers[10:]...)
fmt.Println("删除指定位置的元素后:", numbers)
numbers = append(numbers[:len(numbers)-1], numbers[len(numbers):]...)
fmt.Println("删除切片的最后一个元素:", numbers)
fmt.Println("计算切片的长度:", len(numbers))
var number1 []int
if number1 == nil {
fmt.Println("切片是空的")
}
var countryCapitalMap map[string]string
countryCapitalMap = make(map[string]string)
countryCapitalMap["France"] = "Paris"
countryCapitalMap["Italy"] = "Rome"
countryCapitalMap["Japan"] = "Tokyo"
countryCapitalMap["India"] = "New Delhi"
fmt.Println(countryCapitalMap["India"])
for country := range countryCapitalMap {
fmt.Println("Capital of", country, "is", countryCapitalMap[country])
}
captial, ok := countryCapitalMap["India"]
if ok {
fmt.Println("Capital of India is----", captial)
} else {
fmt.Println("Capital of United States is not present")
}
var phone Phone
phone = new(IPhone)
phone.call()
phone = new(XiaoMi)
phone.call()
}
func testFun1() {
fmt.Println("函数")
}
func testFun2(num1 int, num2 int) {
fmt.Println("函数参数", num1+num2)
}
func testFun3(num1 int, num2 int) int {
return num1 + num2
}
func testFun4(num1 int, num2 int) (int, int) {
return num1 + num2, num1 - num2
}
type Phone interface {
call()
}
type IPhone struct{}
func (iphone IPhone) call() {
fmt.Println("你用的手机是苹果")
}
type XiaoMi struct{}
func (xiaoMi XiaoMi) call() {
fmt.Println("你用的手机是小米: ")
}