Fork me on GitHub

Go编程基础

Go编程基础


1、一般结构
// 当前程序的包名
package main

// 导入其它的包 
import "fmt"

// 常量的定义
const PI = 3.14

// 全局变量的声明与赋值
var name = "gopher"

// 一般类型声明
type newType int

// 结构的声明
type gopher struct{}

// 接口的声明
type golang interface{}

// 由main函数作为程序入口启动
func main() {
	fmt.Println("Hello world!你好,世界!")
}

其它多个简写声明

import (
	"fmt"
	"math"
)

const (
	PI = 3.14
	const1 = "1"
	const2 = 2
)

var (
	name = "gopher"
	name1 = "1"
	name2 = 2
)

type (
	newType int
	type1 = float32
	type2 = string
	type3 = byte
)

2、注释方法

// :单行注释
/* */ :多行注释


3、基本类型
类型 长度 取值范围 说明
布尔型:bool 1字节 true / false 不能用数字代表true或false
整型:int / uint 根据运行平台获取32位或64位
8位整型:int8 / uint8 1字节 -128~127 / 0~255
16位整型:int16 / uint16 2字节 -32768~32767 / 0~65535
32位整型:int32(rune) / uint32 4字节 -2^32 / 2~2^32 / 2-1 / 0~2^32-1
64位整型:int64 / uint64 8字节 -2^64 / 2~2^64 / 2-1 / 0~2^64-1
浮点型:float32 / float64 4 / 8字节 精确到7 / 15小数位
字节型:byte uint8别名

> 复数:complex64 / complex128 ---- 长度8 / 16字节 > 足够保存指针的32位或64位整数型:uintptr > 其它值类型: array、struct、string > 引用类型: slice、map、chan > 接口类型: inteface > 函数类型: func
4、变量的声明与赋值
//单个变量的声明
var a int
//单个变量赋值
a = 1

//单个变量的声明直接赋值
var b int = 2

//单个变量的声明简写赋值
c := 3

//多个变量的声明
var d, e, f int
//多个变量赋值
d, e, f = 1, 2, 3

//省略变量类型,由系统推断
var j, k, l = 5, 6, 7

//多个变量声明与赋值的最简写法
u, i, o :=  8, 9, 10

5、25个关键字

break、default、func、interface、select、case、defer、go、map、struct、chan、else、goto、package、switch、const、fallthrough、if、range、type、continue、for、import、return、var


#### 总结:学习一门的新的语言,确实挺多乐趣,不一样的风格,不一样的写法,慢慢折腾吧,会更加充实。 ####
posted @ 2017-07-24 13:45  大葱哥  阅读(165)  评论(0编辑  收藏  举报