{"id":651,"date":"2024-02-01T13:53:28","date_gmt":"2024-02-01T05:53:28","guid":{"rendered":"https:\/\/www.db2go.net\/?p=651"},"modified":"2024-02-01T13:55:28","modified_gmt":"2024-02-01T05:55:28","slug":"golang%e6%b3%9b%e5%9e%8b%e7%9a%84%e9%ab%98%e7%ba%a7%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.db2go.net\/?p=651","title":{"rendered":"Golang\u6cdb\u578b\u7684\u9ad8\u7ea7\u7528\u6cd5"},"content":{"rendered":"<ol>\n<li>\u6cdb\u578b\u51fd\u6570\u7684\u7ea6\u675f<\/li>\n<\/ol>\n<pre><code class=\"language-go line-numbers\">\/\/ IsPositive \u63a5\u53e3\u7528\u4e8e\u7ea6\u675f\u7c7b\u578b\u5fc5\u987b\u6709 IsPositive \u65b9\u6cd5\ntype IsPositive interface {\n    IsPositive() bool\n}\n\n\/\/ PrintIfPositive \u662f\u4e00\u4e2a\u6cdb\u578b\u51fd\u6570\uff0c\u63a5\u53d7\u5b9e\u73b0\u4e86 IsPositive \u63a5\u53e3\u7684\u53c2\u6570\n\/\/ \u8fd9\u6837\u7684\u7ea6\u675f\u63d0\u9ad8\u4e86\u51fd\u6570\u7684\u901a\u7528\u6027\uff0c\u4f46\u53c8\u786e\u4fdd\u4f20\u5165\u7684\u7c7b\u578b\u5fc5\u987b\u5177\u5907 IsPositive \u65b9\u6cd5\u3002\n\/\/ \u4e0b\u9762\u7684 PositiveInt \u548c PositiveFloat \u7c7b\u578b\u4e0a\uff0c\u5b83\u4eec\u5b9e\u73b0\u4e86 IsPositive \u63a5\u53e3\uff0c\u4ece\u800c\u53ef\u4ee5\u88ab\u4f20\u9012\u7ed9 PrintIfPositive \u51fd\u6570\nfunc PrintIfPositive[T IsPositive](value T) {\n    if value.IsPositive() {\n        fmt.Println(value)\n    }\n}\n\n\/\/ \u6574\u6570\u7c7b\u578b\u5b9e\u73b0\u4e86 IsPositive \u63a5\u53e3\ntype PositiveInt int\n\nfunc (pi PositiveInt) IsPositive() bool {\n    return pi &gt; 0\n}\n\n\/\/ \u6d6e\u70b9\u6570\u7c7b\u578b\u5b9e\u73b0\u4e86 IsPositive \u63a5\u53e3\ntype PositiveFloat float64\n\nfunc (pf PositiveFloat) IsPositive() bool {\n    return pf &gt; 0\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u6cdb\u578b\u7c7b\u578b\u7684\u7ea6\u675f<\/li>\n<\/ol>\n<pre><code class=\"language-go line-numbers\">\/\/ Printer \u63a5\u53e3\u7528\u4e8e\u7ea6\u675f\u7c7b\u578b\u5fc5\u987b\u6709 Print \u65b9\u6cd5\n\/\/ \u7ea6\u675f\u786e\u4fdd\u4f20\u5165\u7684\u7c7b\u578b\u5fc5\u987b\u5177\u5907 Print \u65b9\u6cd5\u3002\ntype Printer interface {\n    Print()\n}\n\n\/\/ PrintPositive \u662f\u4e00\u4e2a\u6cdb\u578b\u51fd\u6570\uff0c\u63a5\u53d7\u5b9e\u73b0\u4e86 Printer \u63a5\u53e3\u7684\u53c2\u6570\nfunc PrintPositive[T Printer](value T) {\n    value.Print()\n}\n\n\/\/ MyType \u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b\u5b9e\u73b0\u4e86 Printer \u63a5\u53e3\n\/\/ \u4ece\u800c\u53ef\u4ee5\u88ab\u4f20\u9012\u7ed9 PrintPositive \u51fd\u6570\u3002\ntype MyType int\n\nfunc (mt MyType) Print() {\n    fmt.Println(mt)\n}\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u6cdb\u578b\u51fd\u6570\u4e0e\u63a5\u53e3\u7ed3\u5408<\/li>\n<\/ol>\n<pre><code class=\"language-go line-numbers\">\/\/ PrintElements \u662f\u4e00\u4e2a\u6cdb\u578b\u51fd\u6570\uff0c\u63a5\u53d7\u5b9e\u73b0\u4e86 Printer \u63a5\u53e3\u7684\u5207\u7247\n\/\/ \u8fd9\u6837\u7684\u8bbe\u8ba1\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u9002\u7528\u4e8e\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u5207\u7247\u3002\nfunc PrintElements[T Printer](elements []T) {\n    for _, element := range elements {\n        element.Print()\n    }\n}\n\n\/\/ IntPrinter \u662f\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u5b9e\u73b0\u4e86 Printer \u63a5\u53e3\ntype IntPrinter int\n\nfunc (ip IntPrinter) Print() {\n    fmt.Println(ip)\n}\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u6cdb\u578b\u7c7b\u578b\u7684\u9012\u5f52<\/li>\n<\/ol>\n<pre><code class=\"language-go line-numbers\">\/\/ TreeNode \u662f\u4e00\u4e2a\u6cdb\u578b\u4e8c\u53c9\u6811\u8282\u70b9\u3002\n\/\/ \u5b83\u5305\u542b\u4e86\u5de6\u53f3\u5b50\u8282\u70b9\uff0c\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u4e5f\u662f TreeNode \u7c7b\u578b\u3002\u8fd9\u79cd\u8bbe\u8ba1\u5728\u6784\u5efa\u901a\u7528\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\u3002\ntype TreeNode[T any] struct {\n    Value       T\n    Left, Right *TreeNode[T]\n}\n<\/code><\/pre>\n<ol start=\"5\">\n<li>\u6cdb\u578b\u4e0e\u53cd\u5c04<\/li>\n<\/ol>\n<pre><code class=\"language-go line-numbers\">\/\/ GenericPrint \u662f\u4e00\u4e2a\u6cdb\u578b\u51fd\u6570\uff0c\u4f7f\u7528\u53cd\u5c04\u6253\u5370\u503c\u548c\u7c7b\u578b\n\/\/ \u8fd9\u4f7f\u5f97\u51fd\u6570\u80fd\u591f\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u4f46\u8981\u6ce8\u610f\u53cd\u5c04\u53ef\u80fd\u5e26\u6765\u4e00\u4e9b\u6027\u80fd\u5f00\u9500\u3002\nfunc GenericPrint[T any](value T) {\n    valueType := reflect.TypeOf(value)\n    fmt.Printf(\"Type: %s, Value: %v\\n\", valueType, value)\n}\n<\/code><\/pre>\n<p>\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">func main() {\n    \/\/ \u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\n    fmt.Println(\"Running tests...\")\n    err := test()\n    if err != nil {\n        fmt.Println(\"Tests failed:\", err)\n    } else {\n        fmt.Println(\"All tests passed.\")\n    }\n}\n\n\n\/\/ \u5355\u5143\u6d4b\u8bd5\n\nfunc test() error {\n    \/\/ 1. \u6cdb\u578b\u51fd\u6570\u7684\u7ea6\u675f\n    PrintIfPositive(PositiveInt(42))     \/\/ \u8f93\u51fa\uff1a42\n    PrintIfPositive(PositiveFloat(3.14)) \/\/ \u8f93\u51fa\uff1a3.14\n    PrintIfPositive(PositiveInt(-10))    \/\/ \u672a\u8f93\u51fa\n\n    \/\/ 2. \u6cdb\u578b\u7c7b\u578b\u7684\u7ea6\u675f\n    PrintPositive(MyType(42)) \/\/ \u8f93\u51fa\uff1a42\n\n    \/\/ 3. \u6cdb\u578b\u51fd\u6570\u4e0e\u63a5\u53e3\u7ed3\u5408\n    PrintElements([]IntPrinter{1, 2, 3, 4, 5}) \/\/ \u8f93\u51fa\uff1a1 2 3 4 5\n\n    \/\/ 4. \u6cdb\u578b\u7c7b\u578b\u7684\u9012\u5f52\n    root := &amp;TreeNode[int]{Value: 1}\n    root.Left = &amp;TreeNode[int]{Value: 2}\n    root.Right = &amp;TreeNode[int]{Value: 3}\n\n    if root.Value != 1 || root.Left.Value != 2 || root.Right.Value != 3 {\n        return fmt.Errorf(\"TreeNode test failed\")\n    }\n\n    \/\/ 5. \u6cdb\u578b\u4e0e\u53cd\u5c04\n    GenericPrint(42)                 \/\/ \u8f93\u51fa\uff1aType: int, Value: 42\n    GenericPrint(\"Hello, Generics!\") \/\/ \u8f93\u51fa\uff1aType: string, Value: Hello, Generics!\n    GenericPrint(3.14)               \/\/ \u8f93\u51fa\uff1aType: float64, Value: 3.14\n\n    return nil\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6cdb\u578b\u51fd\u6570\u7684\u7ea6\u675f \/\/ IsPositive \u63a5\u53e3\u7528\u4e8e\u7ea6\u675f\u7c7b\u578b\u5fc5\u987b\u6709 IsPositive \u65b9\u6cd5 type Is&hellip;<\/p>\n <a href=\"https:\/\/www.db2go.net\/?p=651\" title=\"Golang\u6cdb\u578b\u7684\u9ad8\u7ea7\u7528\u6cd5\" class=\"entry-more-link\"><span>Read More<\/span> <span class=\"screen-reader-text\">Golang\u6cdb\u578b\u7684\u9ad8\u7ea7\u7528\u6cd5<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[12],"tags":[],"class_list":["entry","author-suredandan","post-651","post","type-post","status-publish","format-standard","category-golang"],"views":1699,"_links":{"self":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=651"}],"version-history":[{"count":3,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/651\/revisions"}],"predecessor-version":[{"id":654,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/651\/revisions\/654"}],"wp:attachment":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}