Skip to content

Commit de4b2c8

Browse files
author
snowstorm2stars
committed
init
0 parents  commit de4b2c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2970
-0
lines changed

.editorconfig

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.go]
12+
indent_style = tab
13+
indent_size = 2
14+
15+
[{Makefile, Dockerfile}]
16+
indent_style = tab
17+
indent_size = 4
18+
19+
[{*.yml, *.json}]
20+
indent_style = space
21+
indent_size = 2

.gitignore

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
*.db
6+
7+
# Folders
8+
_obj
9+
_test
10+
bin
11+
.vscode
12+
.idea/
13+
.cache
14+
.envrc
15+
dist
16+
release
17+
18+
# Architecture specific extensions/prefixes
19+
*.[568vq]
20+
[568vq].out
21+
22+
*.cgo1.go
23+
*.cgo2.c
24+
_cgo_defun.c
25+
_cgo_gotypes.go
26+
_cgo_export.*
27+
28+
_testmain.go
29+
30+
*.exe
31+
.DS_Store
32+
33+
# Front
34+
node_modules/
35+
idcshell-ui/
36+
37+
38+
# vim
39+
*.sw[op]

.golangci.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
run:
2+
go: '1.18'
3+
4+
linters:
5+
enable:
6+
- deadcode # Finds unused code
7+
- ineffassign # Detects when assignments to existing variables are not used
8+
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
9+
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
10+
- unused # Checks Go code for unused constants, variables, functions and types
11+
- varcheck # Finds unused global variables and constants
12+
- asciicheck
13+
- depguard
14+
- errorlint
15+
- importas
16+
- tparallel
17+
- gofmt
18+
- bodyclose
19+
20+
disable:
21+
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
22+
- gosimple
23+
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
24+
25+
linters-settings:
26+
govet:
27+
check-shadowing: true
28+
check-unreachable: true
29+
check-rangeloops: true
30+
check-copylocks: true

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) since 2022 The Segmentfault Development Team.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.PHONY: build clean test
2+
3+
GO_ENV=CGO_ENABLED=0
4+
GO_FLAGS=-ldflags="-X main.BuildVersion=$(VERSION) -X 'main.BuildTime=`date`' -extldflags -static"
5+
GO=$(GO_ENV) $(shell which go)
6+
7+
build:
8+
@$(GO) build ./...
9+
10+
test:
11+
@$(GO) test ./...
12+
13+
# clean all build result
14+
clean:
15+
@$(GO) clean ./...

README.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<center>
2+
<img src="docs/logo.png" alt="drawing" width="120" align="center" />
3+
</center>
4+
5+
# Pacman - Yet Another Toolkit to Build Golang Application Quickly
6+
7+
[![LICENSE](https://img.shields.io/badge/License-MIT-green)](https://github.com/segmentfault/pacmam/blob/master/LICENSE)
8+
[![Language](https://img.shields.io/badge/Language-Go-blue.svg)](https://golang.org/)
9+
10+
<!-- TOC depthfrom:2 orderedlist:false -->
11+
12+
- [What is Pacman?](#what-is-pacman)
13+
- [Changelog](#changelog)
14+
- [Design Philosophy](#design-philosophy)
15+
- [Requirements and install](#requirements-and-install)
16+
- [How to Use It](#how-to-use-it)
17+
- [Contributing](#contributing)
18+
- [License](#license)
19+
20+
<!-- /TOC -->
21+
22+
Notice: This document also has [Simplified Chinese 中文版本](README_CN.md) Edition.
23+
24+
## What is Pacman?
25+
26+
[Pacman](https://wiki.archlinux.org/title/Pacman) is the package manager of Arch Linux. The goal of Pacman is to make it possible to easily manage packages.
27+
28+
So the goal of this tool is similar, to make it easier to manage golang third-party libraries while building applications quickly.
29+
30+
## Changelog
31+
32+
`20220919` initial release
33+
34+
## Design Philosophy
35+
36+
Golang is very stable and powerful for server-side application development. But some general issues are confusing and blocking the progress of development, such as:
37+
38+
1. We need many third-party libraries to build the application. We need to choose a suitable one or multiple solutions at the same time, but do not want to be too dependent on them;
39+
2. The upgrade and replacement of third-party dependencies will cause failures, eg. some third-party libraries do not support major version updates.
40+
3. We're building microservices that may require common features, so we don't need to develop them repeatedly.
41+
42+
Based on above the issues, we began to think about how to manage third-party libraries. Far as we are aware of the Spring Boot which famous framework from Java world managing the third-party libraries or dependencies by Bean. This is the appropriate implementation for Ioc - The injection of dependencies.
43+
44+
## Requirements and install
45+
46+
This project requires golang v1.18 or higher version with new features such as general generic and so on.
47+
48+
## How to Use It
49+
50+
To use this toolkit is very simple and easy. For example, we wanna including the general cache module which has implemented:
51+
52+
First, we import the interface from the definition of the cache module.
53+
54+
```go
55+
import "github.com/segmentfault/pacman/cache/v2"
56+
```
57+
58+
Second, we import the implementation from the other path.
59+
60+
```go
61+
import "github.com/segmentfault/pacman/contrib/cache/v2"
62+
```
63+
64+
Finally, we use it!
65+
66+
```go
67+
cache := v2.NewCache[int, string]()
68+
cache.Set(1, "hello")
69+
70+
got, ok := cache.Get(1)
71+
assert.Equal(t, ok, true)
72+
assert.Equal(t, got, "hello")
73+
```
74+
75+
The cache module should be running correctly.
76+
77+
## Contributing
78+
79+
Contributions are always welcome!
80+
81+
See `contributing.md` for ways to get started.
82+
83+
Please adhere to this project's `code of conduct`.
84+
85+
## License
86+
87+
[MIT](https://github.com/segmentfault/pacmam/blob/master/LICENSE)

README_CN.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<center>
2+
<img src="docs/logo.png" alt="drawing" width="120" align="center" />
3+
</center>
4+
5+
# Pacman - 又一个快速搭建 Golang 应用的脚手架
6+
<!-- TOC depthfrom:2 orderedlist:false -->
7+
8+
- [更新记录](#%E6%9B%B4%E6%96%B0%E8%AE%B0%E5%BD%95)
9+
- [设计原则](#%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99)
10+
- [如何使用](#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8)
11+
- [参考资源](#%E5%8F%82%E8%80%83%E8%B5%84%E6%BA%90)
12+
13+
<!-- /TOC -->
14+
15+
这是思否的 Golang 研发团队用于快速搭建的 Golang 应用的开发脚手架。
16+
17+
## 更新记录
18+
19+
`v0.0.5`
20+
21+
基本的文档编写,以及部分代码格式的更改
22+
23+
`v0.0.4(20220829)`
24+
25+
- 入口文件处理,将每个模块各自进行初始化后交由 pacman.App 统一引用
26+
27+
`v0.0.3(20220823)`
28+
29+
- 配置文件获取使用 `viper` 包 ,方便后期扩展
30+
- 更新脚手架初始化依赖使用配置文件调用方法
31+
- 移除不必要的开发文件
32+
33+
## 设计原则
34+
35+
Golang 在后端开发已经相对的成熟,但是在开发过程中往往会困扰我们的:
36+
37+
1. 我们需要选择合适的类库或者解决方案,或者我们想同时尝试多种的方案,但不想过于依赖;
38+
2. 第三方依赖的升级和替换使用会非常的麻烦,而且有可能会失败(例如第三方类库不支持大版本更新等);
39+
3. 不同的微服务其实从复用角度上说有很多通用的场景,这些不必须重复去开发。
40+
41+
种种上述的问题,这需要我们去考虑如何引用、组织以及管理第三方的引用的依赖。其实 Java 已经提供了非常好的思路,例如 Spring Framework 中的 Bean 等等,这些都是 IoC 思想的一种实现。
42+
43+
我们的想法是可能不需要实现那么「笨重」或者「臃肿」,但是能够从 Golang 的角度和思维上来说解决这些问题。因此,就有了这个框架以及对应的代码组织形式。
44+
45+
我们的代码组织形式首先是使用 interface 约定,然后使用 contrib 去实现,从依赖的角度上来说,调用者只只需要关心 interface 的接口以及返回即可,不需要去关心如何实现。因此,可以完整的解决上面提出的两点问题。
46+
47+
## 如何使用
48+
49+
根据设计原则,我们可以调用者角度上来使用这个框架类库,例如我们使用 Cache 组件使用的方式:
50+
51+
首先,我们引入 cache 的 interface,它定义了我们 Cache 接口的形式
52+
53+
```go
54+
import "github.com/segmentfault/pacman/cache/v2"
55+
```
56+
57+
然后,我们引入 contrib 包下面的具体实现(如果有必要,您也可以使用自己的方案实现,参见下章节)
58+
59+
```go
60+
import "github.com/segmentfault/pacman/contrib/cache/v2"
61+
```
62+
63+
然后就是具体的使用,例如我们定义了个非常简单的字符串形式的 Cache
64+
65+
```go
66+
cache := v2.NewCache[int, string]()
67+
cache.Set(1, "hello")
68+
69+
got, ok := cache.Get(1)
70+
assert.Equal(t, ok, true)
71+
assert.Equal(t, got, "hello")
72+
```
73+
74+
对,目前使用方面就是那么的简单!如果想查看详细的细节,可以参考各个模块的具体 README 说明。
75+
76+
## 参考资源
77+
78+
- <https://golangexample.com/simple-and-yet-powerful-dependency-injection-for-golang/>
79+
- <https://zhuanlan.zhihu.com/p/141204279>

cache/cache.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"time"
6+
)
7+
8+
// Deprecated: use generics cache instead
9+
type Cache interface {
10+
GetString(ctx context.Context, key string) (string, error)
11+
SetString(ctx context.Context, key, value string, ttl time.Duration) error
12+
GetInt64(ctx context.Context, key string) (int64, error)
13+
SetInt64(ctx context.Context, key string, value int64, ttl time.Duration) error
14+
Del(ctx context.Context, key string) error
15+
}

cache/v2/cache.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package v2
2+
3+
import "time"
4+
5+
// Cache defines the generics cache interface for 1.8 or above version.
6+
// Notice: SetWithExp function is not necessarily implemented
7+
type Cache[K comparable, V any] interface {
8+
Get(key K) (value V, ok bool)
9+
Set(key K, val V)
10+
SetWithExp(key K, val V, exp time.Duration)
11+
Delete(key K)
12+
}

conf/conf.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package conf
2+
3+
// StaticConfig the config that do not update after application run
4+
// Deprecated: suggested use Config instead
5+
type StaticConfig interface {
6+
// LoadAndSet load config from anywhere and set to conf struct
7+
LoadAndSet(conf interface{}) error
8+
}
9+
10+
// Config interface determines the common methods for parsing configuration from specified resources
11+
type Config interface {
12+
Parse(any) error
13+
Get(string) any
14+
GetBool(string) bool
15+
GetString(string) string
16+
GetInt(string) int
17+
}

contrib/cache/memory/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Memory Cache
2+
> cache data in memory implemented by [go-cache](https://github.com/patrickmn/go-cache)
3+
4+
## Usage
5+
```go
6+
memCache := NewCache()
7+
err := memCache.SetString(context.TODO(), key, val, -1)
8+
```
9+
10+
## Notice
11+
`Save` and `Load` function just for test because `go-cache` `SaveFile` is deprecated.

contrib/cache/memory/go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/segmentfault/pacman/contrib/cache/memory
2+
3+
go 1.18
4+
5+
require (
6+
github.com/patrickmn/go-cache v2.1.0+incompatible
7+
github.com/stretchr/testify v1.8.0
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

contrib/cache/memory/go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
5+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
6+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
10+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
12+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)