Golang 筆記
  • Introduction
  • 安裝Golang
    • 本地編譯與安裝第三方套件
    • 第一個 GO 程式
    • export 與 import
    • 使用 go mod
  • 基本工具
  • DataBase操作
    • MySQL
    • 使用 ORM
    • MongoDB
  • 基本語法
    • Variable
    • BSON
    • JSON
    • 時間相關
    • Interface
    • Error
    • 型別
    • 字串
    • defer
    • panic, recover
    • Channel 與Goroutine
      • 讀寫鎖
      • for select 與 for range
      • UnBuffered channel 與 Buffered channel
    • Function
    • pointer
    • regExp
    • fmt
    • Make vs New
    • struct
    • Array, Slice 陣列
    • map
  • 核心模組
    • Reflect
    • File
    • Signal
    • BuiltIn
    • Sync
    • IO
    • Sort
    • http
    • crypto
    • context
  • 第三方模組
    • Dom Parser
    • gin 框架
    • Websocket
    • Iris框架
      • 讀取 Body 資料
      • 相關範例
    • Fiber 框架
    • 自動重啟 server 工具
    • go-jwt
  • 測試
  • 原始碼解析
  • 常見問題
    • 資料存取不正確
    • Data races
Powered by GitBook
On this page
  • 如果是在不同資料夾則會有package 的概念
  • 注意事項:

Was this helpful?

  1. 安裝Golang

使用 go mod

以前專案都要放在 GOPATH 下,後來出了 govendor,更後來1.11 推出go module.

如果是在同一個資料夾引入的話可以單純把他一起編譯進去即可

go run ./hello.go ./utils.go

如果是在不同資料夾則會有package 的概念

1.以前必須要寫在GOPATH下面,但現在有go mod,可以直接用go mod init <package name>

2. package name要跟資料夾名稱相同,裡面檔案名稱沒差

3.要被引用的func 第一個字母都要大寫

4.我們先新增一個資料夾叫testp,然後裡面放入一個檔案.go 之後在那個資料夾輸入 go mod init testp

testp.go

package testp

import "fmt"

func PrintStart(count int, endNum int) {
	for i := 0; i < endNum-count; endNum-- {
		fmt.Print(" ")
	}
	for i := 0; i < count*2; i++ {
		fmt.Print("*")
	}
	fmt.Print("\r\n")
}

5.然後在main package 也輸入 go mod init main

記得要在main 的 go.mod新增replace testp => ./testp 否則會出現 error

build command-line-arguments: cannot load testp: malformed module path "testp": missing dot in first path element

6. main.go

package main

import (
	"testp"
)

func main() {
	endNum := 20;
	for i := 1; i < endNum; i++ {
		testp.PrintStart(i, endNum);
	}
}

即可

注意事項:

1.記得 go mod init 時 main.go 放在專案資料夾根目錄

2. init 名稱跟資料夾名稱相同

Previousexport 與 importNext基本工具

Last updated 3 years ago

Was this helpful?

golang:專案遷移到 go mod 踩坑記 - 摸鱼
Anatomy of Modules in GoMedium
Logo
Logo