如何在Golang中返回非基本类型变量

时间:2018-10-29 15:26:32

标签: go interface dry

如何在golang中返回非原始类型变量

2 个答案:

答案 0 :(得分:2)

看起来像 mywebView.setDownloadListener(object : DownloadListener { override fun onDownloadStart(url: String, userAgent: String, contentDisposition: String, mimetype: String, contentLength: Long) { val request = DownloadManager.Request(Uri.parse(url)) request.allowScanningByMediaScanner() request.setDescription("Download file...") request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype)) request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE) request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) //Notify client once download is completed! request.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory(), mimetype ) val webview = getSystemService(DOWNLOAD_SERVICE) as DownloadManager webview.enqueue(request) Toast.makeText(getApplicationContext(), "Download avviato", Toast.LENGTH_LONG).show() } }) 返回了bigquery.NewClient,因此,如果您试图封装它,则可以编写一个返回(*Client, error)的函数

答案 1 :(得分:0)

理想情况下,您希望帮助程序以OOPS形式实例化为单例的文件。

package somehelper

type SomeData struct {
    SubData1 SubDataType1
    SubData2 SubDataType2
}

var singleton *SomeData
var once sync.Once

func Createsingleton(data1 SubDataType1, data2 SubDataType2) {
    once.Do(func() {
        singleton = &SomeData{SubData1:data1, SubData2: data2}
    })
}

func GetSingleton() *SomeData {
    if singleton == nil {
        panic("SomeDataHelper needs to be instantiated!!!")
    }
    return singleton
}


func (i *SomeData) GetSubData1() SubData1{
    return i.SubData1
}
相关问题