gob

[Noip2012] 开车旅行

和自甴很熟 提交于 2020-08-14 06:08:45
Description 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为Hi,城市 i 和城市 j 之间的距离 d[i,j]恰好是这两个城市海拔高度之差的绝对值,即d[i,j] = |Hi− Hj|。 旅行过程中,小 A 和小 B 轮流开车,第一天小 A 开车,之后每天轮换一次。他们计划选择一个城市 S 作为起点,一直向东行驶,并且最多行驶 X 公里就结束旅行。小 A 和小 B的驾驶风格不同,小 B 总是沿着前进方向选择一个最近的城市作为目的地,而小 A 总是沿着前进方向选择第二近的城市作为目的地(注意:本题中如果当前城市到两个城市的距离相同,则认为离海拔低的那个城市更近)。如果其中任何一人无法按照自己的原则选择目的城市,或者到达目的地会使行驶的总距离超出 X 公里,他们就会结束旅行。 在启程之前,小 A 想知道两个问题: 对于一个给定的 X=X0,从哪一个城市出发,小 A 开车行驶的路程总数与小 B 行驶的路程总数的比值最小(如果小 B 的行驶路程为 0,此时的比值可视为无穷大,且两个无穷大视为相等)。如果从多个城市出发,小 A 开车行驶的路程总数与小 B 行驶的路程总数的比值都最小,则输出海拔最高的那个城市。 对任意给定的 X=Xi和出发城市 Si,小

go中struct和[]byte互相转换

别来无恙 提交于 2020-04-06 20:37:25
go中struct和[]byte互相转换 binary 包处理二进制 读取将r中的结构化二进制数据读入数据。 数据必须是指向固定大小值或固定大小值切片的指针。 从r读取的字节使用指定的字节顺序进行解码,并写入数据的连续字段。 当解码布尔值时,零字节被解码为假,并且任何其他非零字节被解码为真。 读入结构时,将跳过具有空白(_)字段名称的字段的字段数据; 即,空白字段名称可用于填充。 读入结构时,必须导出所有非空白字段,否则“读取”可能会出现混乱。 但是这种只能使用固定大小类型的数据,变长数据就会异常,例如string, slice 等 package main import ( "bytes" "encoding/binary" "fmt" ) type T struct { A int64 B float64 } func main() { // Create a struct and write it. t := T{A: 0xEEFFEEFF, B: 3.14} buf := &bytes.Buffer{} err := binary.Write(buf, binary.BigEndian, t) if err != nil { panic(err) } fmt.Println(buf.Bytes()) // Read into an empty struct. t = T{}

Efficient Go serialization of struct to disk

放肆的年华 提交于 2019-12-28 04:10:10
问题 I've been tasked to replace C++ code to Go and I'm quite new to the Go APIs. I am using gob for encoding hundreds of key/value entries to disk pages but the gob encoding has too much bloat that's not needed. package main import ( "bytes" "encoding/gob" "fmt" ) type Entry struct { Key string Val string } func main() { var buf bytes.Buffer enc := gob.NewEncoder(&buf) e := Entry { "k1", "v1" } enc.Encode(e) fmt.Println(buf.Bytes()) } This produces a lot of bloat that I don't need: [35 255 129 3

Append to golang gob in a file on disk

。_饼干妹妹 提交于 2019-12-24 02:55:10
问题 I'm trying to save gob -encoded data in a file on disk as a simple datastore. However, when I open it next time the gob encoder just ignores whatever data is already in the file, and starts over sending definitions of already sent formats before sending data. Seeing that gob.Encoder takes an io.Writer , rather than an io.ReadWriter , this makes sense. The encoder has no idea what's in the file, because it cannot read from it. This is, however, quite inefficient. It's also unnecessarily hard

Write to Client UDP Socket in Go

南笙酒味 提交于 2019-12-20 19:04:20
问题 I'm looking for a good solution for a client/server communication with UDP sockets in Go language. The examples I found on the Internet show me how to send data to the server, but they do not teach how to send them back to the client. To demonstrate, my program does the following: My client program creates a socket on the 4444 port, like this: con, err := net.Dial("udp", "127.0.0.1:4444") I sent a string and the local address to the server, so it could print the string and send an OK message.

gob: type not registered for interface: map[string]interface {}

偶尔善良 提交于 2019-12-20 17:30:06
问题 gob fails to encode map[string]interface{} gob: type not registered for interface: map[string]interface {} http://play.golang.org/p/Si4hd8I0JE package main import ( "bytes" "encoding/gob" "encoding/json" "fmt" "log" ) func CloneObject(a, b interface{}) []byte { buff := new(bytes.Buffer) enc := gob.NewEncoder(buff) dec := gob.NewDecoder(buff) err := enc.Encode(a) if err != nil { log.Panic("e1: ", err) } b1 := buff.Bytes() err = dec.Decode(b) if err != nil { log.Panic("e2: ", err) } return b1 }

Unable to send gob data over TCP in Go Programming

巧了我就是萌 提交于 2019-12-20 09:05:20
问题 I have a client server application, using TCP connection Client: type Q struct { sum int64 } type P struct { M, N int64 } func main() { ... //read M and N ... tcpAddr, err := net.ResolveTCPAddr("tcp4", service) ... var p P p.M = M p.N = N err = enc.Encode(p) } Server: type Q struct { sum int64 } type P struct { M, N int64 } func main() { ... tcpAddr, err := net.ResolveTCPAddr("ip4", service) listener, err := net.ListenTCP("tcp", tcpAddr) ... var connB bytes.Buffer dec := gob.NewDecoder(&connB

Gob can't encode map with a nil pointer value

我的未来我决定 提交于 2019-12-13 19:16:26
问题 Gob's Encode returns an error when I try to encode a map to pointers if one of the values is nil. This seems to contradict the docs (but I may be misinterpreting the meaning): In slices and arrays, as well as maps, all elements, even zero-valued elements, are transmitted, even if all the elements are zero. Code: package main import ( "bytes" "encoding/gob" ) type Dog struct { Name string } func main() { m0 := make(map[string]*Dog) m0["apple"] = nil // Encode m0 to bytes var network bytes

Decode gob output without knowing concrete types

别来无恙 提交于 2019-12-13 02:36:23
问题 I'm using gob to serialize structs to disk. The struct in question contains an interface field, so the concrete type needs to be registered using gob.Register(...) . The wrinkle here is that the library doing the gob-ing should be ignorant of the concrete type in use. I wanted the serialization to be possible even when callers have defined their own implementations of the interface. I can successfully encode the data by registering the type on the fly (see trivial example below), but upon

use gob to package recursively defined structs

放肆的年华 提交于 2019-12-07 12:19:40
问题 I mostly use Python, but am playing around with Go. I wrote the following to do something that is quite simple in python, and im hoping it can be accomplished in Go as well. package main import ( "bytes" "encoding/gob" "fmt" "io/ioutil" ) type Order struct { Text string User *User } type User struct { Text string Order *Order } func main() { o := Order{} u := User{} o.Text = "order text" u.Text = "user text" // commenting this section prevents stack overflow o.User = &u u.Order = &o fmt