-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSave.go
279 lines (221 loc) · 5.35 KB
/
Save.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// package main
// import (
// "fmt"
// "net"
// "net/url"
// "strings"
// )
// func main() {
// // s := "postgres://user:[email protected]:5432/path.sf?k=v#f"
// // s := "http://www.google.com/hamzaa/nis.png:8008"
// s := "http://www.google.com/hamzaanis.js"
// u, err := url.Parse(s)
// if err != nil {
// panic(err)
// }
// fmt.Println("Scheme is " + u.Scheme)
// fmt.Println("Host is " + u.Host)
// host, port, _ := net.SplitHostPort(u.Host)
// fmt.Println("Host is " + host)
// fmt.Println(port)
// typee := u.Path[1:]
// filet := strings.Split(typee, ".")
// if filet[len(filet)-1] != "html" {
// fmt.Println("The image is found named " + typee)
// }
// fmt.Printf("Path is %v\n", u.Path)
// }
// // package main
// // import (
// // "fmt"
// // "strings"
// // )
// // func get
// // func main() {
// // url := "www.i.imgur.com/m1UIjW1.jpg"
// // s := strings.Split(url, "/")
// // for _, f := range s {
// // fmt.Printf("%v\n",f)
// // }
// // a:=len(s)
// // fmt.Println(s[a-1])
// // }
// // package main
// // import (
// // "fmt"
// // "io"
// // "log"
// // "net/http"
// // "os"
// // )
// // func main() {
// // url := "http://i.imgur.com/m1UIjW1.jpg"
// // // don't worry about errors
// // response, e := http.Get(url)
// // if e != nil {
// // log.Fatal(e)
// // }
// // defer response.Body.Close()
// // //open a file for writing
// // file, err := os.Create("asdf.jpg")
// // if err != nil {
// // log.Fatal(err)
// // }
// // // Use io.Copy to just dump the response body to the file. This supports huge files
// // _, err = io.Copy(file, response.Body)
// // if err != nil {
// // log.Fatal(err)
// // }
// // file.Close()
// // fmt.Println("Success!")
// // }
// package main
// import "fmt"
// // define Dog object type
// type Dog struct {
// Name string
// Color string
// }
// func main() {
// // create instance of object and set properties
// Spot := Dog{Name: "Spot", Color: "brown"}
// // get pointer of object
// SpotPointer := &Spot
// // modify field through pointer
// SpotPointer.Color = "black"
// fmt.Println(Spot.Color)
// }
// package main
// import (
// "fmt"
// "time"
// )
// func printSlowly(s string, n int) {
// for i := 0; i < n; i++ {
// fmt.Println(i, s)
// time.Sleep(300 * time.Millisecond)
// }
// }
// func main() {
// // This is a normal function call.
// // Main() will finish this off before continuing.
// printSlowly("directly functioning", 3)
// // The go functions below will each spin off to happen each in their own thread,
// // Meaning they'll be called _concurrently_.
// // Calling the named function as a go routine.
// go printSlowly("red fish goroutine", 3)
// go printSlowly("blue fish goroutine", 3)
// // Call an anonymous function as a go routine.
// go func(ss string, nn int) {
// for i := 0; i < nn; i++ {
// fmt.Println(i, ss)
// time.Sleep(150 * time.Millisecond)
// }
// }("anony fish goroutine", 3)
// // Waits for a button to be pushed.
// // Try commenting this!
// var input string
// fmt.Scanln(&input) // Just push RETURN to finish the program.
// fmt.Println("DONE.")
// }
// package main
// import (
// "bytes"
// "encoding/binary"
// "fmt"
// )
// type MyString struct {
// Length int32
// Message [10]byte
// }
// type MyMessage struct {
// First uint64
// Second byte
// _ byte // padding
// Third uint32
// Message MyString
// }
// func main() {
// buf := new(bytes.Buffer)
// a := MyMessage{
// First: 10,
// Second: 10,
// Third: 10,
// Message: MyString{0, [10]byte{'H', 'e', 'l', 'l', 'o', '\n'}},
// }
// b := MyMessage{
// First: 100,
// Second: 0,
// Third: 100,
// Message: MyString{0, [10]byte{'H', 'e', '\n'}},
// }
// test := []MyMessage{a, b}
// fmt.Println(test)
// err := binary.Write(buf, binary.LittleEndian, &test)
// if err != nil {
// fmt.Printf("binary.Read failed:", err)
// return
// }
// // <<--- CONN -->>
// // msg := []MyMessage{}
// msg2 := new(MyMessage)
// err2 := binary.Read(buf, binary.LittleEndian, msg2)
// if err2 != nil {
// fmt.Printf("binary.Read failed:", err2)
// return
// }
// fmt.Println(msg2)
// }
// package main
// import (
// "net/url"
// "github.com/fatih/color"
// )
// func main() {
// _, err := url.ParseRequestURI("http://www.google.com")
// if err != nil {
// color.Red("Invalid URI")
// }
// // link := "http://google.com"
// // u, err := url.ParseRequestURI(link)
// // fmt.Println(u.Scheme + "://" + u.Host)
// // if err != nil {
// // fmt.Println("The rl not correct")
// // }
// // response, err := http.Get(link)
// // if err != nil {
// // log.Fatal(err)
// // } else {
// // defer response.Body.Close()
// // io.Copy(os.Stdout, response.Body)
// // }
// }
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
var m = map[int]string{1: "one", 2: "two", 3: "three"}
var a string = "Hamza"
func main() {
buf := new(bytes.Buffer)
encoder := gob.NewEncoder(buf)
err := encoder.Encode(m)
if err != nil {
panic(err)
}
// your encoded stuff
// fmt.Println(buf.Bytes())
var decodedMap map[int]string
decoder := gob.NewDecoder(buf)
err = decoder.Decode(&decodedMap)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", decodedMap)
call()
}
func call() {
fmt.Println(a)
}