cosmos
Cosmos
search
blog Hello comment
{ "articleTitle": "go 初始化项目", "date": "2022-10-24 15:4:1", "tags": [ "go", "golang" ], "categories": "go", "timestamp": 1666623841000, "readingTime": 0, "outline": "通过 go mod 初始化项目" }
readingTime: <1min

初始化

新建go.mod

xxx为项目名,会生成go.mod文件

go mod init xxx

或直接复制一份go.mod文件

module xxx

go 1.16

新建文件

// 文件路径

--- main.go
--- go.mod
--- first_module
    --- test.go

其中,test.go命名无所谓

文件内容


// main.go
package main

import (
  "fmt"
  "xxx/first_module"
)

func main() {
  first_module.SayHello()
}

// test.go

package first_module

import "fmt"

func SayHello() {
  fmt.Println("hello world")
}
Edit on Github