Last active
March 30, 2021 20:50
-
-
Save dariodip/96dc0c17eb5e7f6b3e9fa37fac976308 to your computer and use it in GitHub Desktop.
Revisions
-
dariodip revised this gist
Mar 30, 2021 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,7 @@ func encode(complexStructures []ComplexStruct) { if err := encoder.Encode(&complexStructures); err != nil { panic(err) } // this is just an example! never write a file inside a function that way if err := os.WriteFile("complex_structures.gob", buf.Bytes(), os.ModePerm); err != nil { panic(err) } -
dariodip revised this gist
Mar 28, 2021 . 3 changed files with 63 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ import ( "crypto/md5" "crypto/sha256" "fmt" "io" ) // ComplexStruct stores a value and its MD5 and SHA256 ingests type ComplexStruct struct { Value string MD5Ingest []byte SHA256Ingest []byte } func NewComplexStruct(v string) ComplexStruct { md5h := md5.New() io.WriteString(md5h, v) sha256h := sha256.New() io.WriteString(sha256h, v) return ComplexStruct{ Value: v, MD5Ingest: md5h.Sum(nil), SHA256Ingest: sha256h.Sum(nil), } } func (cs ComplexStruct) String() string { return fmt.Sprintf("v=%s,md5=%x,sha256=%x", cs.Value, cs.MD5Ingest, cs.SHA256Ingest, ) } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ //go:embed complex_structures.gob var embedCs []byte func main() { var complexStructures []ComplexStruct decoder := gob.NewDecoder(bytes.NewReader(embedCs)) if err := decoder.Decode(&complexStructures); err != nil { panic(err) } for _, cs := range complexStructures { fmt.Println(cs) } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ func encode(complexStructures []ComplexStruct) { var buf bytes.Buffer encoder := gob.NewEncoder(&buf) if err := encoder.Encode(&complexStructures); err != nil { panic(err) } if err := os.WriteFile("complex_structures.gob", buf.Bytes(), os.ModePerm); err != nil { panic(err) } } -
dariodip revised this gist
Mar 28, 2021 . 1 changed file with 18 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ import ( "embed" "fmt" "io/ioutil" ) //go:embed * var fs embed.FS func main() { files, _ := fs.ReadDir(".") for _, file := range files { fmt.Println("File name: ", file.Name()) fileContent, _ := fs.Open(file.Name()) content, _ := ioutil.ReadAll(fileContent) fmt.Println("File content:\n", string(content)) } } -
dariodip revised this gist
Mar 28, 2021 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ import ( "embed" "fmt" ) //go:embed * var f embed.FS func main() { data, _ := f.ReadFile("hello.txt") fmt.Println(string(data)) } -
dariodip created this gist
Mar 28, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ import ( _ "embed" "fmt" ) //go:embed hello.txt var hello []byte func main() { fmt.Println(string(hello)) } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Hello from embedded! 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ import ( _ "embed" "fmt" ) //go:embed hello.txt var hello string func main() { fmt.Println(hello) }