您的位置:首页 > 编程语言 > Go语言

[go语言]基础知识

2013-04-22 22:56 621 查看
一 简单的hello world

HelloWorld

package main

import "fmt"

func main() {
fmt.Println("Hello World!")
var i string
fmt.Scanln(&i)
}


1)packge总会出现

2)import引入包到库中

3)Go程序首先调用main包的main函数

二 语法简介

1 包

1)首先需要在文件里说明包范围package

2)引入使用的包import

3)变量是静态类型的

2 go语言关键字

go语言总共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

3 go语言内建函数

new make 内存分配

len append cap copy 数据结构操作

panic recover 异常处理

print println 打印

close closed 关闭channel

complex real imag 复数处理

4 标准输入输出

1)标准输入读取

input := make([]byte, 1024)

os.Stdin.Read(input)

println(string(input[0:len(input)-1]))

2)标准输入,使用ioutil读取

input, _ := ioutil.ReadAll(os.Stdin);

println(string(input[0:len(input)-1]))

2)标准输入使用缓冲流读取

reader := bufio.NewReader(os.Stdin)

input, _ := reader.ReadBytes('\n')

println(string(input[0:len(input)-1])) // string(input[0:len(input)-1]) remove '\n'.

注释

Go提供C风格的 /* */ 块注释和C++风格的 // 行注释。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: