package test import ( "fmt" "os" "testing" "github.com/foxglove/go-rosbag" ) func TestWriteRosBag(t *testing.T) { // Check if the file exists, if not create it f, err := os.OpenFile("my-bag.bag", os.O_RDWR|os.O_CREATE, 0644) if err != nil { panic(err) } defer func(f *os.File) { err := f.Close() if err != nil { fmt.Printf("Error closing file: %s\n", err) } }(f) writer, err := rosbag.NewWriter(f) if err != nil { panic(err) } err = writer.WriteConnection(&rosbag.Connection{ Conn: 1, Topic: "/foo", Data: rosbag.ConnectionHeader{ Topic: "/foo", Type: "Foo", MD5Sum: "abc", MessageDefinition: []byte{0x01, 0x02, 0x03}, //// optional fields //CallerID: &callerID, //Latching: &latching, }, }) if err != nil { panic(err) } for i := 0; i < 100; i++ { data := "hello" err = writer.WriteMessage(&rosbag.Message{ Conn: 0, Time: uint64(i), Data: []byte(data), }) if err != nil { panic(err) } fmt.Println("保存数据", data) } }