函数参数中的 Golang 函数数组

Golang Array of functions in function argument

玩弄 golang parquet 包并发现函数 s3.NewS3FileWriter 接受以下参数 []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)
}

[]func(*s3manager.Uploader)是什么意思?以及如何传递 s3Manager.Uploader 的修改版本?例如,如果我想更改默认值。

调用时NewS3FileWriteruploaderOptions参数需要用函数片段填充。并且每个函数都必须有一个类型为 *s3manager.Uploader.

的参数

示例:

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)