golang 的一些学习和参考

135 阅读2分钟

样板工程可以参考

1.github.com/apache/incu… 2. cloudwego的使用 先把环境变量启动

export PATH=$PATH:$(go env GOPATH)/bin
source ~/.bashrc 

安装cloudwego:

go install github.com/cloudwego/cwgo@latest
go install github.com/cloudwego/thriftgo@latest

golang 项目:

https://github.com/eyebluecn 都在这个项目下
https://github.com/eyebluecn/sc-misc

golang vscode 配置: 具体参考的文章 :juejin.cn/post/712189…

image.png 然后

{
  // 使用 IntelliSense 了解相关属性。
  // 悬停以查看现有属性的描述。
  // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Package",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${fileDirname}",
      "cwd": "${workspaceFolder}",
      "env": {},
      "args": []
    }
  ]
}

如果是执行Test

{
    "configurations": [
        {
            "name": "Test Current File",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "program": "${fileDirname}",
            "cwd": "${workspaceFolder}" // 强制工作目录为项目根目录
        }
    ]
}

更换go verison


curl -sSL https://github.com/soulteary/gvm/raw/master/binscripts/gvm-installer | bash
gvm install go1.23.6 -B
gvm use go1.23.6
然后通过设置golang 扩展来设计插件
Go: Choose Go Environment

防止golang 里面没有实现接口的一种方法

type Fly interface {
  take_off(way string)
}


var _,Fly=  *(bird)(nill) 这一句来验证是否完全基础
type bird struct {
  
}
func(srv *bird) take_off(way string){
} 

golang 里面实现继承的方法 如果父类实现了接口的方法 那么子类默认就能实现、同时可以改写方法

type abstract  struct{
  

}

type child strct{
 *abstract 
}

自定义实现 父类

type Service interface {
    Foobar(a string) (string, error)
}

type ServiceAbstract struct{}

func (s *ServiceAbstract) Foobar(a string) (string, error) {
    return "", nil // 默认实现
}

type ServiceImpl struct {
    *ServiceAbstract // 内嵌基类
}

// 覆盖基类的 Foobar 方法
func (s *ServiceImpl) Foobar(a string) (string, error) {
    return "impl", nil // 具体实现
}

学习一下open code 代码心得 比如使用泛型来实现不同大模型客户端

https://github.com/charmbracelet/crush  里面的provider.go 这个函数写得非常的设计得很巧妙 使用了桥接设计模式 可以用在后面写不同的客户端实现的使用上比如mcp 不同协议的时候使用泛型+加上桥接的模型 外部提供和内部实现两个+来搞定这个、同时在opt 里面参数和方法分离的方式也能写得更加轻便

opt 的一种写法

可以,把它当成“一个会修改配置的函数类型”就行。下面是最小可运行 demo,展示 func(*Settings) 这种写法(Option 模式)是怎么工作的:                                               
                                                                                                                                                                                
    package main                                                                                                                                                                
                                                                                                                                                                                
    import "fmt"                                                                                                                                                                
                                                                                                                                                                                
    type Settings struct{ Name string; Retry int }                                                                                                                              
    type Option = func(*Settings) // 关键:函数类型,参数是 *Settings                                                                                                           
                                                                                                                                                                                
    func WithName(name string) Option { return func(s *Settings) { s.Name = name } }                                                                                            
    func WithRetry(n int) Option      { return func(s *Settings) { s.Retry = n } }                                                                                              
                                                                                                                                                                                
    func NewClient(opts ...Option) Settings {                                                                                                                                   
        s := Settings{Name: "default", Retry: 1}                                                                                                                                
        for _, o := range opts { o(&s) } // 把 s 的“地址”传进去,让 option 直接改它                                                                                             
        return s                                                                                                                                                                
    }                                                                                                                                                                           
                                                                                                                                                                                
    func main() {                                                                                                                                                               
        c := NewClient(WithName("alice"), WithRetry(3))                                                                                                                         
        fmt.Printf("%+v\n", c) // 输出: {Name:alice Retry:3}                                                                                                                    
    }    

框架学习

  1. github.com/Mulily0513/… 这个golang 代码值得好好学习如何写一个框架
  2. github.com/Altergom/go… 这个实现agent 很好呢