dqitk20644 2019-07-05 09:00
浏览 210
已采纳

Golang函数参数中的函数数组

Playing around with golang parquet package and came across the function s3.NewS3FileWriter that accepts the following arguement []func(*s3manager.Uploader)

func NewS3FileWriter(
    ctx context.Context,
    bucket string,
    key string,
    uploaderOptions []func(*s3manager.Uploader),
    cfgs ...*aws.Config,
) (source.ParquetFile, error) {
    if activeS3Session == nil {
        sessLock.Lock()
        if activeS3Session == nil {
            activeS3Session = session.Must(session.NewSession())
        }
        sessLock.Unlock()
    }

    file := &S3File{
        ctx:             ctx,
        client:          s3.New(activeS3Session, cfgs...),
        writeDone:       make(chan error),
        uploaderOptions: uploaderOptions,
        BucketName:      bucket,
        Key:             key,
    }

    return file.Create(key)
}

What does []func(*s3manager.Uploader) mean? And how do I pass a modified version of s3Manager.Uploader? If I wanted to change the defaults for example.

Thanks

  • 写回答

1条回答 默认 最新

  • duan33360 2019-07-05 09:45
    关注

    The uploaderOptions argument of NewS3FileWriter need to be filled with slice of functions during call. And each of the functions must have an argument with type of *s3manager.Uploader.

    Example:

    uploaderOptions := make([]func(*s3manager.Uploader), 0)
    
    uploader1 := func (param *s3manager.Uploader) {
        fmt.Println("uploader 1", param)
    }
    uploaderOptions = append(uploaderOptions, uploader1)
    
    uploader2 := func (param *s3manager.Uploader) {
        fmt.Println("uploader 2", param)
    }
    uploaderOptions = append(uploaderOptions, uploader2)
    
    // ...
    
    NewS3FileWriter(ctx, bucket, key, uploaderOptions)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VS2022 C++的相关代码问题咨询
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥30 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码