@kjk1/

gobook file copy

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8626003818109329","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"Ly8gbm8gb3V0cHV0CnBhY2thZ2UgbWFpbgoKaW1wb3J0ICgKCSJpbyIKCSJsb2ciCgkib3MiCikKCi8vIDpzaG93IHN0YXJ0Ci8vIENvcHlGaWxlIGNvcGllcyBhIHNyYyBmaWxlIHRvIGRzdApmdW5jIENvcHlGaWxlKGRzdCwgc3JjIHN0cmluZykgZXJyb3IgewoJc3JjRmlsZSwgZXJyIDo9IG9zLk9wZW4oc3JjKQoJaWYgZXJyICE9IG5pbCB7CgkJcmV0dXJuIGVycgoJfQoJZGVmZXIgc3JjRmlsZS5DbG9zZSgpCgoJZHN0RmlsZSwgZXJyIDo9IG9zLkNyZWF0ZShkc3QpCglpZiBlcnIgIT0gbmlsIHsKCQlyZXR1cm4gZXJyCgl9CglfLCBlcnIgPSBpby5Db3B5KGRzdEZpbGUsIHNyY0ZpbGUpCgllcnIyIDo9IGRzdEZpbGUuQ2xvc2UoKQoJaWYgZXJyID09IG5pbCAmJiBlcnIyICE9IG5pbCB7CgkJZXJyID0gZXJyMgoJfQoJaWYgZXJyICE9IG5pbCB7CgkJLy8gZGVsZXRlIHRoZSBkZXN0aW5hdGlvbiBpZiBjb3B5IGZhaWxlZAoJCW9zLlJlbW92ZShkc3QpCgl9CglyZXR1cm4gZXJyCn0KCi8vIDpzaG93IGVuZAoKZnVuYyBtYWluKCkgewoJc3JjIDo9ICJtYWluLmdvIgoJZHN0IDo9ICJtYWluX2NvcHkuZ28iCgllcnIgOj0gQ29weUZpbGUoZHN0LCBzcmMpCglpZiBlcnIgIT0gbmlsIHsKCQlsb2cuRmF0YWxmKCJDb3B5RmlsZSBmYWlsZWQgd2l0aCAnJXMnXG4iLCBlcnIpCgl9Cglvcy5SZW1vdmUoZHN0KQp9"},"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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// no output
package main

import (
	"io"
	"log"
	"os"
)

// :show start
// CopyFile copies a src file to dst
func CopyFile(dst, src string) error {
	srcFile, err := os.Open(src)
	if err != nil {
		return err
	}
	defer srcFile.Close()

	dstFile, err := os.Create(dst)
	if err != nil {
		return err
	}
	_, err = io.Copy(dstFile, srcFile)
	err2 := dstFile.Close()
	if err == nil && err2 != nil {
		err = err2
	}
	if err != nil {
		// delete the destination if copy failed
		os.Remove(dst)
	}
	return err
}

// :show end

func main() {
	src := "main.go"
	dst := "main_copy.go"
	err := CopyFile(dst, src)
	if err != nil {
		log.Fatalf("CopyFile failed with '%s'\n", err)
	}
	os.Remove(dst)
}