Created
September 11, 2018 00:25
-
-
Save renanregis/d77a47092735269eb400960b74f99043 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"path/filepath" | |
) | |
//File representation | |
type File struct { | |
Name string `json:"Name"` | |
Path string `json:"Path"` | |
} | |
//Files array | |
type Files []File | |
func main() { | |
_ = jsonify("data") | |
} | |
func jsonify(dir string) error { | |
var file Files | |
err := filepath.Walk(dir, | |
func(path string, info os.FileInfo, err error) error { | |
if info.IsDir() { | |
return nil | |
} | |
file = append(file, File{info.Name(), "./" + path}) | |
return nil | |
}) | |
filesJSON, err := json.Marshal(file) | |
if err != nil { | |
log.Fatalln("Cannot encode to JSON", err) | |
} | |
fmt.Println(string(filesJSON)) | |
toJSON(string(filesJSON)) | |
return nil | |
} | |
func toJSON(str string) { | |
var jsonBlob = []byte(str) | |
data := Files{} | |
err := json.Unmarshal(jsonBlob, &data) | |
if err != nil { | |
log.Fatalln("Cannot unmarshal json blob", err.Error()) | |
} | |
messageJSON, _ := json.Marshal(data) | |
err = ioutil.WriteFile("files.json", messageJSON, 0644) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment