@kjk1/

gobook file get size

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.9019264981032062","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"Ly8gbm8gcGxheWdyb3VuZApwYWNrYWdlIG1haW4KCmltcG9ydCAoCgkiZm10IgoJImxvZyIKCSJvcyIKKQoKLy8gOnNob3cgc3RhcnQKCi8vIEdldEZpbGVTaXplIHJldHVybnMgZmlsZSBzaXplIG9yIGVycm9yIGlmIGUuZy4gZmlsZSBkb2Vzbid0IGV4aXN0CmZ1bmMgR2V0RmlsZVNpemUocGF0aCBzdHJpbmcpIChpbnQ2NCwgZXJyb3IpIHsKCXN0LCBlcnIgOj0gb3MuTHN0YXQocGF0aCkKCWlmIGVyciAhPSBuaWwgewoJCXJldHVybiAtMSwgZXJyCgl9CglyZXR1cm4gc3QuU2l6ZSgpLCBuaWwKfQoKZnVuYyBtYWluKCkgewoJcGF0aCA6PSAibWFpbi5nbyIKCXNpemUsIGVyciA6PSBHZXRGaWxlU2l6ZShwYXRoKQoJaWYgZXJyICE9IG5pbCB7CgkJbG9nLkZhdGFsZigiR2V0RmlsZVNpemUgZmFpbGVkIHdpdGggJyVzJ1xuIiwgZXJyKQoJfQoJZm10LlByaW50ZigiRmlsZSAlcyBpcyAlZCBieXRlcyBpbiBzaXplXG4iLCBwYXRoLCBzaXplKQp9CgovLyA6c2hvdyBlbmQ="},"asBuffer":null},"loaded":true}}
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
// no playground
package main

import (
	"fmt"
	"log"
	"os"
)

// :show start

// GetFileSize returns file size or error if e.g. file doesn't exist
func GetFileSize(path string) (int64, error) {
	st, err := os.Lstat(path)
	if err != nil {
		return -1, err
	}
	return st.Size(), nil
}

func main() {
	path := "main.go"
	size, err := GetFileSize(path)
	if err != nil {
		log.Fatalf("GetFileSize failed with '%s'\n", err)
	}
	fmt.Printf("File %s is %d bytes in size\n", path, size)
}

// :show end