函數中輸入參數的可變類型
php小編蘋果為您介紹函數中輸入參數的可變型別。在php中,函數的參數型別可以是固定的,也可以是可變的。可變類型參數,指的是函數可以接受不同類型的參數作為輸入,這在處理不同場景下的資料非常有用。透過使用特殊的參數標識符,例如“...”,我們可以在函數定義中聲明可變類型參數。這使得我們能夠更靈活地處理各種不同類型的數據,提高程式碼的複用性和可讀性。無論是字串、數字、陣列或是其他類型,我們都可以輕鬆地將它們作為參數傳遞給函數,並在函數內部進行相應的處理。這種靈活的參數類型處理方式,讓我們的程式碼更加健壯和適應性強,能夠應付各種複雜的業務需求。
問題內容
我建立了一個函數來取得使用者對拉取請求的最後評論。我正在使用“github.com/google/go-github/github”包。我想將它用於 []*github.issuecomment 和 []*github.pullrequestcomment 類型。有沒有辦法使輸入參數的類型可變,這樣我就不必在函數定義中指定它,並且可以使用任一類型呼叫函數?
func getlastuserinteractionpr(comments_array *github.issuecomment or *github.pullrequestcomment)(*github.issuecomment or *github.pullrequestcomment) { }
泛型的使用:
func getlastuserinteractionpr(comments_array any)(any) { }
這是一個緊急解決方案,因為我正在做的整個專案都是用 go 1.14 編寫的,而這個功能可以從 go 1.18 中獲得
當我嘗試使用空介面{}作為輸入類型時:
#func getLastUserInteractionPRIssue(comments_array interface{})(*github.IssueComment) { comments_array []*github.IssueComment(comments_array); err { fmt.Println("success") } else { fmt.Println("failure") } }
解決方法
你關心例如的內部結構嗎? issuecomment
?
type issuecomment struct { id *int64 `json:"id,omitempty"` nodeid *string `json:"node_id,omitempty"` body *string `json:"body,omitempty"` user *user `json:"user,omitempty"` reactions *reactions `json:"reactions,omitempty"` createdat *time.time `json:"created_at,omitempty"` updatedat *time.time `json:"updated_at,omitempty"` // authorassociation is the comment author's relationship to the issue's repository. // possible values are "collaborator", "contributor", "first_timer", "first_time_contributor", "member", "owner", or "none". authorassociation *string `json:"author_association,omitempty"` url *string `json:"url,omitempty"` htmlurl *string `json:"html_url,omitempty"` issueurl *string `json:"issue_url,omitempty"` }
例如,您關心從中提取某些特定欄位嗎? pullrequestcomment
是一個更大的結構(它有更多欄位),您關心從中提取一些欄位嗎?
或您只想要每個的字串表示形式?您要做什麼很大程度上取決於您想如何處理傳遞的值。
如果您只想要每個string
表示,您可以使用極端(老實說,不是很安全- 我不推薦這個)範例,讓您的函數接受fmt.stringer
物件的切片: p>
func dostuffwithstringifiedcomments(cs []fmt.stringer) { // both issuecomment and pullrequestcomment provide string() // methods and therefore implement fmt.stringer for _, comment := range cs { dosomethingwith(comment.string()) } }
您的切片現在可以包含任一類型的對象,並且不會發生任何爆炸。缺點:它還可能包含數以億計的其他類型,但沒有一個是您想要的。因此,您需要新增類型斷言檢查:
switch t := comment.(type) { case github.issuecomment: // do good stuff case github.pullrequestcomment: // do other good stuff default: // yell and scream about the value of t }
如果您想要提取某些字段,則必須組合一個採用[]interface{}
之類的函數(使其成為空介面的一部分,而不是空介面代表切片),迭代它並對切片的每個元素進行類型檢查,並提取任何有意義的字段,只要該元素屬於您期望的類型即可:
func DoStuff(comments []interface{}) error { for _, c : = range comments { if ic, ok := c.(*github.IssueComment); ok { // Remove the deref if your slice contains values, not references // Pull out fields and do IssueComment-specific things ProcessIssueComment(ic) } else if prc, ok := c.(*github.PullRequestComment); ok { // Do PRComment-specific things ProcessPullRequestComment(prc) } else { return(fmt.Errorf("I did not want something of type %s", t)) } } return nil }
另外:遊說專案擁有者(如果不是您)遷移到目前版本的 go。 1.14 到 2020 年才發布,但這對於 go 版本來說已經是永恆的了。
以上是函數中輸入參數的可變類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

Golang在並發性上優於C ,而C 在原始速度上優於Golang。 1)Golang通過goroutine和channel實現高效並發,適合處理大量並發任務。 2)C 通過編譯器優化和標準庫,提供接近硬件的高性能,適合需要極致優化的應用。

goimpactsdevelopmentpositationality throughspeed,效率和模擬性。 1)速度:gocompilesquicklyandrunseff,IdealforlargeProjects.2)效率:效率:ITScomprehenSevestAndardArdardArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增強的Depleflovelmentimency.3)簡單性。

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

Golang適合快速開發和並發場景,C 適用於需要極致性能和低級控制的場景。 1)Golang通過垃圾回收和並發機制提升性能,適合高並發Web服務開發。 2)C 通過手動內存管理和編譯器優化達到極致性能,適用於嵌入式系統開發。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。Golang以其并发模型和高效性能著称,Python则以简洁语法和丰富库生态系统著称。

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。
