亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

golang 中的結構概念需要幫助

golang 中的結構概念需要幫助

Go
ibeautiful 2023-06-26 16:31:31
我想在 NewNotifier 函數中使用 slacknotificationprovider 。我該怎么做。我還想在 newNotifier 函數中發送一個字符串(config.Cfg.SlackWebHookURL)。我應該怎么辦?另外請給我推薦一些材料,以更深入地了解 golang 中的結構和接口。我還想知道為什么 ProviderType.Slack 沒有定義,正如我在 SlackNotificationProvider 類型的 ProviderType 結構中提到的那樣?謝謝。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 參數中的 ProviderType 類型 2. ProviderType.Slack 未定義(ProviderType 類型沒有 Slack 方法)go
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

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


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”內容很好,我看到其他人已經鏈接了它。也就是說,具有一個方法的結構感覺應該是一個函數,您也可以將其定義為一種類型以允許您傳回一些內容。例子:


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")

}



查看完整回答
反對 回復 2023-06-26
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號