-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-filesystem-operations.html
45 lines (45 loc) · 12.6 KB
/
go-filesystem-operations.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!doctype html><html lang=en-uk><head><script data-goatcounter=https://ruivieira-dev.goatcounter.com/count async src=//gc.zgo.at/count.js></script><script src=https://unpkg.com/@alpinejs/intersect@3.x.x/dist/cdn.min.js></script><script src=https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js></script><script type=module src=https://ruivieira.dev/js/deeplinks/deeplinks.js></script><link rel=preload href=https://ruivieira.dev/lib/fonts/fa-brands-400.woff2 as=font type=font/woff2 crossorigin=anonymous><link rel=preload href=https://ruivieira.dev/lib/fonts/fa-regular-400.woff2 as=font type=font/woff2 crossorigin=anonymous><link rel=preload href=https://ruivieira.dev/lib/fonts/fa-solid-900.woff2 as=font type=font/woff2 crossorigin=anonymous><link rel=preload href=https://ruivieira.dev/fonts/firacode/FiraCode-Regular.woff2 as=font type=font/woff2 crossorigin=anonymous><link rel=preload href=https://ruivieira.dev/fonts/vollkorn/Vollkorn-Regular.woff2 as=font type=font/woff2 crossorigin=anonymous><link rel=stylesheet href=https://ruivieira.dev/css/kbd.css type=text/css><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><title>Go filesystem operations · Rui Vieira</title>
<link rel=canonical href=https://ruivieira.dev/go-filesystem-operations.html><meta name=viewport content="width=device-width,initial-scale=1"><meta name=robots content="all,follow"><meta name=googlebot content="index,follow,snippet,archive"><meta property="og:title" content="Go filesystem operations"><meta property="og:description" content="Notes on Go filesystem operations.
Copying filesGo does not have an utility method to copy files. We have to rely on writing our own implementation using the reading and writing functionality in other packages. As an example:
package main import ( "io" "log" "os" ) func main() { from, err := os.Open("./foo.txt") if err != nil { log.Fatal(err) } defer from.Close() to, err := os.OpenFile("./bar.txt", os.O_RDWR|os.O_CREATE, 0666) if err != nil { log."><meta property="og:type" content="article"><meta property="og:url" content="https://ruivieira.dev/go-filesystem-operations.html"><meta property="article:section" content="posts"><meta property="article:modified_time" content="2023-05-28T11:45:56+01:00"><meta name=twitter:card content="summary"><meta name=twitter:title content="Go filesystem operations"><meta name=twitter:description content="Notes on Go filesystem operations.
Copying filesGo does not have an utility method to copy files. We have to rely on writing our own implementation using the reading and writing functionality in other packages. As an example:
package main import ( "io" "log" "os" ) func main() { from, err := os.Open("./foo.txt") if err != nil { log.Fatal(err) } defer from.Close() to, err := os.OpenFile("./bar.txt", os.O_RDWR|os.O_CREATE, 0666) if err != nil { log."><link rel=stylesheet href=https://ruivieira.dev/css/styles.css><!--[if lt IE 9]><script src=https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js></script><script src=https://oss.maxcdn.com/respond/1.4.2/respond.min.js></script><![endif]--><link rel=icon type=image/png href=https://ruivieira.dev/images/favicon.ico></head><body class="max-width mx-auto px3 ltr" x-data="{currentHeading: undefined}"><div class="content index py4"><div id=header-post><a id=menu-icon href=#><i class="fas fa-eye fa-lg"></i></a>
<a id=menu-icon-tablet href=#><i class="fas fa-eye fa-lg"></i></a>
<a id=top-icon-tablet href=# onclick='$("html, body").animate({scrollTop:0},"fast")' style=display:none aria-label="Top of Page"><i class="fas fa-chevron-up fa-lg"></i></a>
<span id=menu><span id=nav><ul><li><a href=https://ruivieira.dev/>Home</a></li><li><a href=https://ruivieira.dev/blog/>Blog</a></li><li><a href=https://ruivieira.dev/draw/>Drawings</a></li><li><a href=https://ruivieira.dev/map/>All pages</a></li><li><a href=https://ruivieira.dev/search.html>Search</a></li></ul></span><br><div id=share style=display:none></div><div id=toc><h4>Contents</h4><nav id=TableOfContents><ul><li><a href=#copying-files :class="{'toc-h2':true, 'toc-highlight': currentHeading == '#copying-files' }">Copying files</a></li><li><a href=#path-operations :class="{'toc-h2':true, 'toc-highlight': currentHeading == '#path-operations' }">Path operations</a></li><li><a href=#basepath :class="{'toc-h3':true, 'toc-highlight': currentHeading == '#basepath' }">Basepath</a></li><li><a href=#check-if-directory-exists :class="{'toc-h3':true, 'toc-highlight': currentHeading == '#check-if-directory-exists' }">Check if directory exists</a></li><li><a href=#create-nested-directories :class="{'toc-h3':true, 'toc-highlight': currentHeading == '#create-nested-directories' }">Create nested directories</a></li></ul></nav><h4>Related</h4><nav><ul><li class="header-post toc"><span class=backlink-count>1</span>
<a href=https://ruivieira.dev/go.html>Go</a></li></ul></nav></div></span></div><article class=post itemscope itemtype=http://schema.org/BlogPosting><header><h1 class=posttitle itemprop="name headline">Go filesystem operations</h1><div class=meta><div class=postdate>Updated <time datetime="2023-05-28 11:45:56 +0100 BST" itemprop=datePublished>2023-05-28</time>
<span class=commit-hash>(<a href=https://ruivieira.dev/log/index.html#1454e0a>1454e0a</a>)</span></div></div></header><div class=content itemprop=articleBody><p>Notes on Go filesystem operations.</p><h2 id=copying-files x-intersect="currentHeading = '#copying-files'">Copying files</h2><p>Go does not have an utility method to copy files. We have to rely on writing our own implementation using the reading and writing functionality in other packages. As an example:</p><div class=highlight><pre tabindex=0 style=background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-go data-lang=go><span style=display:flex><span><span style=font-weight:700>package</span> main
</span></span><span style=display:flex><span>
</span></span><span style=display:flex><span><span style=font-weight:700>import</span> (
</span></span><span style=display:flex><span> <span style=color:#b84>"io"</span>
</span></span><span style=display:flex><span> <span style=color:#b84>"log"</span>
</span></span><span style=display:flex><span> <span style=color:#b84>"os"</span>
</span></span><span style=display:flex><span>)
</span></span><span style=display:flex><span>
</span></span><span style=display:flex><span><span style=font-weight:700>func</span> <span style=color:#900;font-weight:700>main</span>() {
</span></span><span style=display:flex><span> from, err <span style=font-weight:700>:=</span> os.<span style=color:#900;font-weight:700>Open</span>(<span style=color:#b84>"./foo.txt"</span>)
</span></span><span style=display:flex><span> <span style=font-weight:700>if</span> err <span style=font-weight:700>!=</span> <span style=font-weight:700>nil</span> {
</span></span><span style=display:flex><span> log.<span style=color:#900;font-weight:700>Fatal</span>(err)
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span> <span style=font-weight:700>defer</span> from.<span style=color:#900;font-weight:700>Close</span>()
</span></span><span style=display:flex><span>
</span></span><span style=display:flex><span> to, err <span style=font-weight:700>:=</span> os.<span style=color:#900;font-weight:700>OpenFile</span>(<span style=color:#b84>"./bar.txt"</span>, os.O_RDWR|os.O_CREATE, <span style=color:#099>0666</span>)
</span></span><span style=display:flex><span> <span style=font-weight:700>if</span> err <span style=font-weight:700>!=</span> <span style=font-weight:700>nil</span> {
</span></span><span style=display:flex><span> log.<span style=color:#900;font-weight:700>Fatal</span>(err)
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span> <span style=font-weight:700>defer</span> to.<span style=color:#900;font-weight:700>Close</span>()
</span></span><span style=display:flex><span>
</span></span><span style=display:flex><span> _, err = io.<span style=color:#900;font-weight:700>Copy</span>(to, from)
</span></span><span style=display:flex><span> <span style=font-weight:700>if</span> err <span style=font-weight:700>!=</span> <span style=font-weight:700>nil</span> {
</span></span><span style=display:flex><span> log.<span style=color:#900;font-weight:700>Fatal</span>(err)
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span>}
</span></span></code></pre></div><h2 id=path-operations x-intersect="currentHeading = '#path-operations'">Path operations</h2><h3 id=basepath x-intersect="currentHeading = '#basepath'">Basepath</h3><p>To get the basepath of a path string we use the <a href=https://golang.org/pkg/path/#Dir>Dir</a> method:</p><div class=highlight><pre tabindex=0 style=background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-go data-lang=go><span style=display:flex><span>filepath.<span style=color:#900;font-weight:700>Dir</span>(<span style=color:#b84>"/etc/foo/file.txt"</span>) <span style=color:#998;font-style:italic>// "/etc/foo"
</span></span></span></code></pre></div><h3 id=check-if-directory-exists x-intersect="currentHeading = '#check-if-directory-exists'">Check if directory exists</h3><div class=highlight><pre tabindex=0 style=background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-go data-lang=go><span style=display:flex><span><span style=font-weight:700>if</span> _, err <span style=font-weight:700>:=</span> os.<span style=color:#900;font-weight:700>Stat</span>(<span style=color:#b84>"/etc/foo/"</span>); os.<span style=color:#900;font-weight:700>IsNotExist</span>(err) {
</span></span><span style=display:flex><span> <span style=color:#998;font-style:italic>// do something because it does not exist
</span></span></span><span style=display:flex><span><span style=color:#998;font-style:italic></span>}
</span></span></code></pre></div><h3 id=create-nested-directories x-intersect="currentHeading = '#create-nested-directories'">Create nested directories</h3><p>Use <a href=https://golang.org/pkg/os/#MkdirAll>MkdirAll</a>:</p><div class=highlight><pre tabindex=0 style=background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-go data-lang=go><span style=display:flex><span>os.<span style=color:#900;font-weight:700>MkdirAll</span>(<span style=color:#b84>"/etc/long/nested/path/to/create"</span>, os.ModePerm)
</span></span></code></pre></div></div></article><div id=footer-post-container><div id=footer-post><div id=nav-footer style=display:none><ul><li><a href=https://ruivieira.dev/>Home</a></li><li><a href=https://ruivieira.dev/blog/>Blog</a></li><li><a href=https://ruivieira.dev/draw/>Drawings</a></li><li><a href=https://ruivieira.dev/map/>All pages</a></li><li><a href=https://ruivieira.dev/search.html>Search</a></li></ul></div><div id=toc-footer style=display:none><nav id=TableOfContents><ul><li><a href=#copying-files>Copying files</a></li><li><a href=#path-operations>Path operations</a><ul><li><a href=#basepath>Basepath</a></li><li><a href=#check-if-directory-exists>Check if directory exists</a></li><li><a href=#create-nested-directories>Create nested directories</a></li></ul></li></ul></nav></div><div id=share-footer style=display:none></div><div id=actions-footer><a id=menu-toggle class=icon href=# onclick='return $("#nav-footer").toggle(),!1' aria-label=Menu><i class="fas fa-bars fa-lg" aria-hidden=true></i> Menu</a>
<a id=toc-toggle class=icon href=# onclick='return $("#toc-footer").toggle(),!1' aria-label=TOC><i class="fas fa-list fa-lg" aria-hidden=true></i> TOC</a>
<a id=share-toggle class=icon href=# onclick='return $("#share-footer").toggle(),!1' aria-label=Share><i class="fas fa-share-alt fa-lg" aria-hidden=true></i> share</a>
<a id=top style=display:none class=icon href=# onclick='$("html, body").animate({scrollTop:0},"fast")' aria-label="Top of Page"><i class="fas fa-chevron-up fa-lg" aria-hidden=true></i> Top</a></div></div></div><footer id=footer><div class=footer-left>Copyright © 2024 Rui Vieira</div><div class=footer-right><nav><ul><li><a href=https://ruivieira.dev/>Home</a></li><li><a href=https://ruivieira.dev/blog/>Blog</a></li><li><a href=https://ruivieira.dev/draw/>Drawings</a></li><li><a href=https://ruivieira.dev/map/>All pages</a></li><li><a href=https://ruivieira.dev/search.html>Search</a></li></ul></nav></div></footer></div></body><link rel=stylesheet href=https://ruivieira.dev/css/fa.min.css><script src=https://ruivieira.dev/js/jquery-3.6.0.min.js></script><script src=https://ruivieira.dev/js/mark.min.js></script><script src=https://ruivieira.dev/js/main.js></script></html>