{"id":8262,"date":"2023-01-29T14:47:20","date_gmt":"2023-01-29T06:47:20","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262"},"modified":"2024-08-23T17:10:06","modified_gmt":"2024-08-23T09:10:06","slug":"%e6%96%87%e4%bb%b6%e5%b8%b8%e7%94%a8%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262","title":{"rendered":"\u6587\u4ef6\u5e38\u7528\u64cd\u4f5c"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262\/#os%E5%8C%85\" title=\"os\u5305\">os\u5305<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262\/#%E5%85%B6%E5%AE%83\" title=\"\u5176\u5b83\">\u5176\u5b83<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262\/#%E6%96%87%E4%BB%B6%E7%BC%96%E8%BE%91\" title=\"\u6587\u4ef6\u7f16\u8f91\">\u6587\u4ef6\u7f16\u8f91<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262\/#%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%E6%88%AA%E5%8F%96\" title=\"\u6587\u4ef6\u76ee\u5f55\u622a\u53d6\">\u6587\u4ef6\u76ee\u5f55\u622a\u53d6<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262\/#%E6%9B%BF%E6%8D%A2%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E6%8C%87%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2\" title=\"\u66ff\u6362\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u4e32\">\u66ff\u6362\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u4e32<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262\/#%E6%96%87%E4%BB%B6%E6%8B%B7%E8%B4%9D\" title=\"\u6587\u4ef6\u62f7\u8d1d\">\u6587\u4ef6\u62f7\u8d1d<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262\/#go-homedir%E5%BA%93\" title=\"go-homedir\u5e93\">go-homedir\u5e93<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=8262\/#%E5%B0%86%E6%96%87%E4%BB%B6%E5%85%A8%E9%83%A8%E8%AF%BB%E5%8F%96%E5%88%B0%E4%B8%80%E4%B8%AAstring%E4%B8%AD\" title=\"\u5c06\u6587\u4ef6\u5168\u90e8\u8bfb\u53d6\u5230\u4e00\u4e2astring\u4e2d\">\u5c06\u6587\u4ef6\u5168\u90e8\u8bfb\u53d6\u5230\u4e00\u4e2astring\u4e2d<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"os%E5%8C%85\"><\/span>os\u5305<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\n<pre><code class=\"language-go\">\npackage main<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>import (<br \/>\n&quot;fmt&quot;<br \/>\n&quot;os&quot;<br \/>\n)<\/p>\n<p>func main() {<br \/>\n\/\/ \u6587\u4ef6\u8def\u5f84<br \/>\npath := &quot;.\/example.txt&quot;<\/p>\n<pre><code>\/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\nif _, err := os.Stat(path); os.IsNotExist(err) {\n    \/\/ \u6587\u4ef6\u4e0d\u5b58\u5728\n    fmt.Printf(&quot;\u6587\u4ef6 %s \u4e0d\u5b58\u5728\\n&quot;, path)\n} else {\n    \/\/ \u6587\u4ef6\u5b58\u5728\n    fmt.Printf(&quot;\u6587\u4ef6 %s \u5b58\u5728\\n&quot;, path)\n}<\/code><\/pre>\n<p>}<\/p>\n<pre><code>\n- \u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728\nos.stat()\n\n- \u6587\u4ef6\u91cd\u547d\u540d\u4e0e\u5220\u9664\nos\u5305Rename\u3001Remove\u3001Mkdir\n\n- \u6587\u4ef6\u4e0d\u5b58\u5728\u81ea\u52a8\u521b\u5efa\n\n```go\n\/\/ \u5982\u679c path \u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u4f1a\u6709 err\uff0c\u7136\u540e\u901a\u8fc7 IsNotExist \u5224\u5b9a\u6587\u4ef6\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c true \u5219\u4e0d\u5b58\u5728\uff0c\u6ce8\u610f\u7528 os.ModePerm \u8fd9\u6837\u6587\u4ef6\u662f\u53ef\u4ee5\u5199\u5165\u7684\nif _, err := os.Stat(path); os.IsNotExist(err) {\n    \/\/ mkdir \u521b\u5efa\u76ee\u5f55\uff0cmkdirAll \u53ef\u521b\u5efa\u591a\u5c42\u7ea7\u76ee\u5f55\n    os.MkdirAll(productPrePath, os.ModePerm)\n}\n<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E5%85%B6%E5%AE%83\"><\/span>\u5176\u5b83<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E6%96%87%E4%BB%B6%E7%BC%96%E8%BE%91\"><\/span>\u6587\u4ef6\u7f16\u8f91<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/blog.csdn.net\/youngwhz1\/article\/details\/89706704\">https:\/\/blog.csdn.net\/youngwhz1\/article\/details\/89706704<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%E6%88%AA%E5%8F%96\"><\/span>\u6587\u4ef6\u76ee\u5f55\u622a\u53d6<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\u548c\u7236\u76ee\u5f55<\/li>\n<\/ul>\n<pre><code class=\"language-go\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;log&quot;\n    &quot;os&quot;\n    &quot;path\/filepath&quot;\n    &quot;strings&quot;\n)\n\nfunc substr(s string, pos, length int) string {\n    runes := []rune(s)\n    l := pos + length\n    if l &gt; len(runes) {\n        l = len(runes)\n    }\n    return string(runes[pos:l])\n}\n\nfunc getParentDirectory(dirctory string) string {\n    return substr(dirctory, 0, strings.LastIndex(dirctory, &quot;\/&quot;))\n}\n\nfunc getCurrentDirectory() string {\n    dir, err := filepath.Abs(filepath.Dir(os.Args[0]))\n    if err != nil {\n        log.Fatal(err)\n    }\n    return strings.Replace(dir, &quot;\\\\&quot;, &quot;\/&quot;, -1)\n}\n\nfunc main() {\n\n    var str1, str2 string\n    str1 = getCurrentDirectory()\n\n    str2 = getParentDirectory(str1)\n    fmt.Println(str2)\n\n}<\/code><\/pre>\n<p>\u4f7f\u7528filepath\u5e93\u66f4\u597d<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%9B%BF%E6%8D%A2%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E6%8C%87%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2\"><\/span>\u66ff\u6362\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u4e32<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/www.qetool.com\/scripts\/view\/4155.html\">https:\/\/www.qetool.com\/scripts\/view\/4155.html<\/a><\/p>\n<pre><code class=\"language-go\">package main \nimport (\n    &quot;bufio&quot;\n    &quot;fmt&quot;\n    &quot;io&quot;\n    &quot;os&quot;\n    &quot;strings&quot;\n)\n\nfunc main() {\n    if len(os.Args) != 4 {\n        fmt.Println(&quot;lack of config file, eg: go run main.go ${path_of_file} ${old_string} ${new_string}&quot;)\n        os.Exit(-1)\n    }\n    fileName := os.Args[1]\n    in, err := os.Open(fileName)\n    if err != nil {\n        fmt.Println(&quot;open file fail:&quot;, err)\n        os.Exit(-1)\n    }\n    defer in.Close()\n\n    out, err := os.OpenFile(fileName+&quot;.mdf&quot;, os.O_RDWR|os.O_CREATE, 0766)\n    if err != nil {\n        fmt.Println(&quot;Open write file fail:&quot;, err)\n        os.Exit(-1)\n    }\n    defer out.Close()\n\n    br := bufio.NewReader(in)\n    index := 1\n    for {\n        line, _, err := br.ReadLine()\n        if err == io.EOF {\n            break\n        }\n        if err != nil {\n            fmt.Println(&quot;read err:&quot;, err)\n            os.Exit(-1)\n        }\n        newLine := strings.Replace(string(line), os.Args[2], os.Args[3], -1)\n        _, err = out.WriteString(newLine + &quot;\\n&quot;)\n        if err != nil {\n            fmt.Println(&quot;write to file fail:&quot;, err)\n            os.Exit(-1)\n        }\n        fmt.Println(&quot;done &quot;, index)\n        index++\n    }\n    fmt.Println(&quot;FINISH!&quot;)\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u6709\u95ee\u9898\uff0c\u8f93\u5165\u4e0e\u8f93\u51fa\u4e0d\u80fd\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%96%87%E4%BB%B6%E6%8B%B7%E8%B4%9D\"><\/span>\u6587\u4ef6\u62f7\u8d1d<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/660757280\">https:\/\/zhuanlan.zhihu.com\/p\/660757280<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"go-homedir%E5%BA%93\"><\/span>go-homedir\u5e93<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000021585053\">https:\/\/segmentfault.com\/a\/1190000021585053<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%B0%86%E6%96%87%E4%BB%B6%E5%85%A8%E9%83%A8%E8%AF%BB%E5%8F%96%E5%88%B0%E4%B8%80%E4%B8%AAstring%E4%B8%AD\"><\/span>\u5c06\u6587\u4ef6\u5168\u90e8\u8bfb\u53d6\u5230\u4e00\u4e2astring\u4e2d<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/imangodoc.com\/fZYIUX4P.html\">https:\/\/imangodoc.com\/fZYIUX4P.html<\/a><\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;io\/ioutil&quot;\n)\n\nfunc main() {\n    \/\/ \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n    content, err := ioutil.ReadFile(&quot;filename.txt&quot;)\n    if err != nil {\n        panic(err)\n    }\n\n    \/\/ \u8f93\u51fa\u6587\u4ef6\u5185\u5bb9\n    fmt.Println(string(content))\n}\n<\/code><\/pre>\n<button class=\"simplefavorite-button\" data-postid=\"8262\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 os\u5305\u5176\u5b83\u6587\u4ef6\u7f16\u8f91\u6587\u4ef6\u76ee\u5f55\u622a\u53d6\u66ff\u6362\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u4e32\u6587\u4ef6\u62f7\u8d1dgo-homedir\u5e93\u5c06\u6587\u4ef6\u5168\u90e8\u8bfb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[617],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/8262"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8262"}],"version-history":[{"count":13,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/8262\/revisions"}],"predecessor-version":[{"id":14459,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/8262\/revisions\/14459"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8262"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}