go語言中的接口實現

type Reader interface {
  Read(p []byte) (n int, err error)
}

 

type MyIoReader struct{
    UploadFileSize int
    UploadeSize *int//這樣用是因爲Read不能用指針this,要不編譯不過
    Count int//test (this *MyIoReader) and (this MyIoReader)
}

//這樣表示是類型MyIoReader實現了io.Reader接口, var instance io.Reader = MyIoReader
//這個p怎麼就成了輸入參數,應該是輸出的參數,原理上是這個參數是指針型的既可做輸入又可做輸出
// 在minio的上傳接口minio.PutObjectOptions中的Progress參數的接口中,p代表每次上傳了的字節數據
func (this MyIoReader) Read(p []byte) (int, error) {
    log.Info(" this is MyIoReader")
    log.Infof("write len:", len(p))
    log.Infof("upload_size1:", *this.UploadeSize)
    *(this.UploadeSize) += len(p)
    log.Infof("upload_size2:", *this.UploadeSize)
    log.Info("Count:", this.Count)
    return 0, nil
}

/*
這樣表示是類型*MyIoReader實現了io.Reader接口, var instance io.Reader = new(MyIoReader)
func (this *MyIoReader) Read(p []byte) (int, error) {
    log.Info(" this is MyIoReader")
    log.Infof("write len:", len(p))
    log.Infof("upload_size1:", *this.UploadeSize)
    *(this.UploadeSize) += len(p)
    log.Infof("upload_size2:", *this.UploadeSize)
    return 0, nil
}
*/

//這個表示傳的是MyIoReader類型對象的指針
func (this *MyIoReader) MyFun1(add int) {
    log.Infof("count1:", this.Count)
    this.Count += add
    log.Infof("count2:", this.Count)
}
//這個表示傳的是MyIoReader類型對象的一個拷貝對象(新生成的臨時對象,當函數退出就會被垃圾回收)
func (this MyIoReader) MyFun2(add int) {
    log.Infof("count1:", this.Count)
    this.Count += add
    log.Infof("count2:", this.Count)
}


type MyIoReader2 struct{
    UploadFileSize int
    UploadeSize int//這樣用是因爲Read不能用指針this,要不編譯不過
}
//*MyIoReqder2才實現了io.Reader接口,而MyIoReader2是沒實現io.Reader接口的
func (this *MyIoReader2) Read(p []byte) (int, error) {
    log.Info(" this is MyIoReader")
    log.Infof("write len:", len(p))
    log.Infof("upload_size1:", this.UploadeSize)
    this.UploadeSize += len(p)
    log.Infof("upload_size2:", this.UploadeSize)
    return 0, nil
}

 

測試使用示例:

    var reader MyIoReader
    reader.UploadeSize = new(int)
    *(reader.UploadeSize) = 0
    reader.Count = 101

    url, err := PutLocalFileV2("/mnt/hgfs/share/CentOS-7-x86_64-Minimal-1810.iso",  "dir_test/hello/file.iso", reader)
    log.Infof("%#v, %#v", url, err)
    

    /* 
    //*MyIoReqder2才實現了io.Reader接口,而MyIoReader2是沒實現io.Reader接口的
    var reader2 MyIoReader2
    reader2.UploadeSize = 0
    url, err := PutLocalFileV2("/mnt/hgfs/share/CentOS-7-x86_64-Minimal-1810.iso",  "dir_test/hello/file.iso", &reader2)
    log.Infof("%#v, %#v", url, err)
    */

 

對應封裝函數

func PutLocalFileV2(local_file string, object_name string, progress io.Reader) (string, error) {

    minio_client, err := minio.New(EndPoint, AccessKey, AccessSecret, UseSSL)
    if err != nil {
        log.Error(err)
        return "", err
    }

    // 上傳一個文件。
    file_suffix := path.Ext(local_file)
    if "" == file_suffix {
        file_suffix = ".noSuffix"
    }

    content_type := "application/" + string([]byte(file_suffix)[1:])
    bucket_name := BucketName

    // 使用FPutObject上傳一個文件。
    n, err := minio_client.FPutObject(bucket_name, object_name, local_file, minio.PutObjectOptions{ContentType: content_type, Progress: progress})
    if err != nil {
        log.Error(err)
        return "", err
    }

    log.Info("Successfully uploaded "+object_name+" of size:", n)
    url := PutObjUrlHead + "/" + BucketName
    if url[len(url)-1] != '/' {
        url = url + "/"
    }
    url = url + object_name
    return url, nil
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章