45

Go 语言程序设计(1)

 5 years ago
source link: https://studygolang.com/articles/18798?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

记录点:

godoc -http=localhost:6060
go version
go build
go install
go get

HelloWorld 代码:

package main

import (
    "fmt"
    "os"
    "strings"
)

func main() {
    who := "World!"
    if len(os.Args) > 1 {
        who = strings.Join(os.Args[1:], " ")
    }
    fmt.Println("Hello", who)
}

HelloWorld 代码解读:

  • 每一个 Go 程序都必须包含一个 main 包,并且含有一个 main 函数
  • Go 语言针对的是包,而非文件,意味着一个包可以拆分层多个文件,只需要这些文档的包声明一致
  • import 可以单次导入,也可以批量导入,建议使用批量导入
  • Go 语言不建议使用分号分隔,这些会由编译器自动实现
  • 函数内可以使用快速变量声明 := ,函数外这不行,但是可以使用 var 声明变量
  • len() 方法可以获得长度
  • os 包提供了操作系统函数的不依赖平台的接口, Args 方法保管了命令行参数,第一个是程序名
  • [n:] 返回了从第 n 个元素到最后一个元素
  • strings 包实现了用于操作字符的简单函数
  • fmt 包实现了实现了类似C语言 printfscanf 的格式化 I/O

Go 语言学习相关的站点:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK