Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

学习 Go (Part 1: hello world) #314

Open
nonocast opened this issue Jul 23, 2022 · 0 comments
Open

学习 Go (Part 1: hello world) #314

nonocast opened this issue Jul 23, 2022 · 0 comments

Comments

@nonocast
Copy link
Owner

nonocast commented Jul 23, 2022

version: go1.18.3 darwin/arm64

Hello World

main.go

package main

import "fmt"

func main() {
	fmt.Println("hello world")
}

Makefile

default: app

run: app
	./$^

app: main.go
	go build -o $@ $^

clean:
	rm app

.PHONY: run clean
  • 编译和运行: make && make run
  • 检查arch file app: app: Mach-O 64-bit executable arm64

说明:

  • 通过os.Exit将return value给操作系统
  • 通过os.Args获取命令行参数
  • vscode 会自动import package, 不用手动输入import "fmt"

Package

  • 基本复用模块单元: 以首字母大写来表明可被包外代码访问
  • 代码的package name可以和所在目录不一致
  • 同一目录里Go代码的package必须一致

注:

  • import "fmt" or import "foo/bar": 这里的import后面是path
  • fmt.Println: 这里的fmt是package name,即文件第一行声明的包名

Module

Go关于模块的信息很乱,前后的变化差异很大,这里一定只能看官网文档。我现在说也不等于你看的时候对,所以还是直接去官网看文档最靠谱。

参考课程和文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant