Skip to content

dk900912/simple-map

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

如何发布个人开发的模块

github上建立代码仓库

克隆代码仓库

[email protected]:dk900912/simple-map.git

编写代码

package sorted

type SortedMap struct {
	Keys   []string
	Values []int
}

func (sm *SortedMap) Set(key string, value int) {
	sm.Keys = append(sm.Keys, key)
	sm.Values = append(sm.Values, value)
}

func (sm *SortedMap) Get(key string) (int, bool) {
	for i, k := range sm.Keys {
		if k == key {
			return sm.Values[i], true
		}
	}
	return 0, false
}
package sorted

import "testing"

func TestSortedMap(t *testing.T) {
	sm := &SortedMap{}

	// 测试 Set 方法
	sm.Set("key1", 1)
	sm.Set("key2", 2)

	// 测试 Get 方法
	value, ok := sm.Get("key1")
	if !ok || value != 1 {
		t.Errorf("Expected value 1 for key1, got %d, ok: %v", value, ok)
	}

	value, ok = sm.Get("key2")
	if !ok || value != 2 {
		t.Errorf("Expected value 2 for key2, got %d, ok: %v", value, ok)
	}

	// 测试不存在的键
	value, ok = sm.Get("key3")
	if ok {
		t.Errorf("Expected key3 to not exist, but got value: %d", value)
	}

	// 测试插入顺序
	sm.Set("key3", 3)
	expectedKeys := []string{"key1", "key2", "key3"}
	for i, key := range expectedKeys {
		if sm.Keys[i] != key {
			t.Errorf("Expected key %s at index %d, got %s", key, i, sm.Keys[i])
		}
	}
}

初始化Go模块

go mod init github.com/dk900912/simple-map

提交到代码仓库

提交代码到远程仓库
git push origin main:main

其他项目使用该Go模块

首先,在代码中直接引入该模块即可,如下所示:
package main

import (
	"fmt"
	sm "github.com/dk900912/simple-map/sorted"
)

func main() {
	sortedmap := &sm.SortedMap{}
	sortedmap.Set("a", 1)
	sortedmap.Set("b", 2)
	fmt.Println(sortedmap.Get("a"))
}

然后,下载该Go模块,如下所示:

hello-module> go get github.com/dk900912/simple-map
go: downloading github.com/dk900912/simple-map v0.0.0-20250115025209-168bc3f7b649
go: added github.com/dk900912/simple-map v0.0.0-20250115025209-168bc3f7b649

最后,go.mod内容已经被自动追加了依赖,如下所示:

module hello-module

go 1.22.0

require github.com/dk900912/simple-map v0.0.0-20250115025209-168bc3f7b649 // indirect

疑问一:`v0.0.0-20250115025209-168bc3f7b649`有什么含义

v0.0.0-20250115025209-168bc3f7b649是Go自动生成的版本信息,中间是git commit time,尾部是长度40位git commit id的前12位。

疑问二:为什么是`v0.0.0`开头

因为我们刚才提交代码的时候,一是没有在本地打标签,二是没有没有将标签提交到远程仓库。接下来,咱们来完善这两步,如下:
hello-module> git push origin --tags
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:dk900912/simple-map.git
 * [new tag]         v1.0.0 -> v1.0.0

紧接着,再次执行go get,如下所示:

hello-module> go get github.com/dk900912/simple-map
go: downloading github.com/dk900912/simple-map v1.0.0
go: upgraded github.com/dk900912/simple-map v0.0.0-20250115025209-168bc3f7b649 => v1.0.0
module hello-module

go 1.22.0

require github.com/dk900912/simple-map v1.0.0 // indirect

最后,如果simple-map这一Go模块又追加了一次提交,但并没有发布新的Tag,而我们hello-module项目中有需要该最新追加提交的功能,此时我们可以通过12位git commit id来获取,你会发现版本号自动变为了v1.0.1

> go get github.com/dk900912/simple-map@c2d95cbcd20f
go: downloading github.com/dk900912/simple-map v1.0.1-0.20250115033734-c2d95cbcd20f
go: upgraded github.com/dk900912/simple-map v1.0.0 => v1.0.1-0.20250115033734-c2d95cbcd20f
module hello-module

go 1.22.0

require github.com/dk900912/simple-map v1.0.1-0.20250115033734-c2d95cbcd20f // indirect

疑问一与疑问二中提到的**v0.0.0****v1.0.1**是具有普适性的,只要符合文中这两种场景,那么伪版本号中的第一部分就具有这样的规律!!!

继续发布v2.0.0

一定要更新`go.mod`文件,即以`v2`作为后缀。
module github.com/dk900912/simple-map/v2

如果只是单纯git tag v2.0.0,后续试图引入该模块的main module会报错的,报错信息如下:

hello-module> go get github.com/dk900912/simple-map@v2.0.0
go: github.com/dk900912/simple-map@v2.0.0: reading https://mirrors.tencent.com/go/github.com/dk900912/simple-map/@v/v2.0.0.info: 404 Not Found
        server response:
        go mod download -json github.com/dk900912/simple-map@v2.0.0:
        {
                "Path": "github.com/dk900912/simple-map",
                "Version": "v2.0.0",
                "Error": "github.com/dk900912/[email protected]: invalid version: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2"
        }

主要就是因为,你认为你打了v2.0.0的标签且已发布到代码仓库了,但是由于没有在go.mod文件中声明v2,那么Go工具链还是认为这是一个v1版本:invalid version: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2。

不仅go get会报错,当你直接把require github.com/dk900912/simple-map v2.0.0手动加入到模块文件中时,依然会报错:version "v2.0.0" invalid: should be v0 or v1, not v2。

继续发布v3.0.0

删除go.mod文件,详细指令如下:
simple-map> git commit -a -m "del mod file"
simple-map> git tag v3.0.0
simple-map> git push origin main --tags

接下来,我们在hello-module这一main module来引入v3.0.0simple-map

package main

import (
	"fmt"
	sm "github.com/dk900912/simple-map/sorted"
)

func main() {
	sortedmap := &sm.SortedMap{}
	sortedmap.Set("a", 1)
	sortedmap.Set("b", 2)
	fmt.Println(sortedmap.Get("a"))
	fmt.Println(sortedmap.Get("a"))
}
hello-module>go get github.com/dk900912/[email protected]
go: downloading github.com/dk900912/simple-map v3.0.0+incompatible
go: added github.com/dk900912/simple-map v3.0.0+incompatible

最后,看一下hello-module下的go.mod,确实看到了期望看到的incompatible

module hello-module

go 1.22.0

require (
	github.com/dk900912/simple-map v3.0.0+incompatible // indirect
)

在 Go 语言中,当你看到 +incompatible 的标记时,这通常意味着所下载的模块没有遵循 Go Modules 的版本控制规范。具体来说,这种情况可能发生在以下几种情况下:

  1. 没有 go.mod 文件:如果模块的根目录中没有 go.mod 文件,Go 将无法确定该模块的版本,因此会将其标记为不兼容。
  2. 使用的版本不符合语义版本控制:Go Modules 期望模块遵循语义版本控制(SemVer)。如果模块的版本号不符合 SemVer 的格式(例如,版本号实际已经大于等于 2 但是却不以 v2或者v3开头),Go 可能会将其视为不兼容。

About

simple map implementations

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages