第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

golang 中的結(jié)構(gòu)概念需要幫助

golang 中的結(jié)構(gòu)概念需要幫助

Go
ibeautiful 2023-06-26 16:31:31
我想在 NewNotifier 函數(shù)中使用 slacknotificationprovider 。我該怎么做。我還想在 newNotifier 函數(shù)中發(fā)送一個字符串(config.Cfg.SlackWebHookURL)。我應(yīng)該怎么辦?另外請給我推薦一些材料,以更深入地了解 golang 中的結(jié)構(gòu)和接口。我還想知道為什么 ProviderType.Slack 沒有定義,正如我在 SlackNotificationProvider 類型的 ProviderType 結(jié)構(gòu)中提到的那樣?謝謝。type SlackNotificationProvider struct {    SlackWebHookURL string    PostPayload     PostPayload}type ProviderType struct {    Slack   SlackNotificationProvider    Discord DiscordNotificationProvider}type Notifier interface {    SendNotification() error}func NewNotifier(providerType ProviderType) Notifier {    if providerType == ProviderType.Slack {        return SlackNotificationProvider{            SlackWebHookURL: SlackWebHookURL,        }    } else if providerType == ProviderType.Discord {        return DiscordNotificationProvider{            DiscordWebHookURL: SlackWebHookURL + "/slack",        }    }    return nil}slackNotifier := NewNotifier(config.Cfg.SlackWebHookURL)錯誤: 1. 無法使用 config.Cfg.SlackWebHookURL(字符串類型)作為 NewNotifiergo 參數(shù)中的 ProviderType 類型 2. ProviderType.Slack 未定義(ProviderType 類型沒有 Slack 方法)go
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經(jīng)驗 獲得超7個贊

Golang 是一種強類型語言,這意味著函數(shù)的參數(shù)是已定義的并且不能不同。字符串是字符串并且只是字符串,結(jié)構(gòu)是結(jié)構(gòu)并且只是結(jié)構(gòu)。接口是 golang 的說法“這可以是具有具有以下簽名的方法的任何結(jié)構(gòu)”。因此,您不能將 astring作為 a傳遞ProviderType,并且您的結(jié)構(gòu)都沒有實際實現(xiàn)您定義的接口方法,因此沒有任何東西可以像您所布置的那樣工作。要將您所掌握的內(nèi)容重新組織成可能有效的內(nèi)容:


const (

    discordType = "discord"

    slackType = "slack"

)


// This means this will match any struct that defines a method of 

// SendNotification that takes no arguments and returns an error

type Notifier interface {

    SendNotification() error

}


type SlackNotificationProvider struct {

    WebHookURL string

}


// Adding this method means that it now matches the Notifier interface

func (s *SlackNotificationProvider) SendNotification() error {

    // Do the work for slack here

}


type DiscordNotificationProvider struct {

   WebHookURL string

}


// Adding this method means that it now matches the Notifier interface

func (s *DiscordNotificationProvider) SendNotification() error {

    // Do the work for discord here

}


func NewNotifier(uri, typ string) Notifier {

    switch typ {

    case slackType:

       return SlackNotificationProvider{

            WebHookURL: uri,

        }

    case discordType:

        return DiscordNotificationProvider{

            WebHookURL: uri + "/slack",

        }

    }

    return nil

}

// you'll need some way to figure out what type this is

// could be a parser or something, or you could just pass it

uri := config.Cfg.SlackWebHookURL

typ := getTypeOfWebhook(uri)

slackNotifier := NewNotifier(uri, typ)

就幫助解決此問題的文檔而言,“Go By Examples”內(nèi)容很好,我看到其他人已經(jīng)鏈接了它。也就是說,具有一個方法的結(jié)構(gòu)感覺應(yīng)該是一個函數(shù),您也可以將其定義為一種類型以允許您傳回一些內(nèi)容。例子:


type Foo func(string) string


func printer(f Foo, s string) {

    fmt.Println(f(s))

}


func fnUpper(s string) string {

    return strings.ToUpper(s)

}


func fnLower(s string) string {

    return strings.ToLower(s)

}


func main() {

   printer(fnUpper, "foo")

   printer(fnLower, "BAR")

}



查看完整回答
反對 回復(fù) 2023-06-26
  • 1 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號