[go语言]基础知识

来源:互联网 发布:什么小说软件免费 编辑:程序博客网 时间:2024/06/05 09:06

一 简单的hello world
HelloWorld
package mainimport "fmt"func main() {fmt.Println("Hello World!")var i stringfmt.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++风格的 // 行注释。
原创粉丝点击