I'm rather new to Golang and not sure yet, how to use certain language constructs. Currently I have following code (with test debug outputs), which does not provide expected result:
json, _ := json.Marshal(struct)
fmt.Println(json)
f,_ := os.Create(fmt.Sprintf("/tmp/%s.json", "asd"))
i,_ := f.Write(json)
fmt.Println(i)
b, err := ioutil.ReadAll(f)
fmt.Print(b)
I expect the following behaviour:
translating the struct to a byte array
creating a new file
append the byte array to the file
However, the file is always empty when I run the code in my environment (AWS Lambda), as well as using it in the Golang Playground.
The output of above code looks like this:
[123 34 ... <hug array of bytes>]
1384
[]
which leads me to believe I'm using f.Write() not correctly, although I followed the package documentation. All other outputs indicate expected behavior, so what is my mistake? I'm somewhat restricted to using the File interface, otherwise I'd have gone with ioutil.WriteFile(). My assumption is a misunderstanding of pointer/values at some point, but the compiler prevented a usage of &f.
After f.Write(), your current position in the file is at the end of it, so ioutil.ReadAll() will read from that position and return nothing.
You need to call f.Sync() to make sure that the data is persistently saved to the disk, and then f.Seek(0, 0) to rewind to the beginning of the file first.
Update: from comments, it seems that you only need to serialize the JSON and pass it forward as io.Reader, for that you don't really need a file, thanks to bytes.Buffer:
data, _ := json.Marshal(s)
buf := bytes.NewBuffer(data)
b, _ := ioutil.ReadAll(buf)
fmt.Print(string(b))
Related
I have a simple piece of code where I want to convert elements of a slice into json and then print them out to my http.responseWriter.
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
However when I compile this I get the error "result declared and not used". If I add a simple line:
_ = result
Then everything compiles and works fine. Why does the compiler complain about this usage, and what is the correct way to do this in go?
Any insight is appreciated, my searches so far seem to indicate the call to Fprintf should count as a usage.
The code in question does not result in the error posted, for proof, check it on the Go Playground.
This error usually is (and the op confirmed it is too in this case) caused by having a local variable with same name outside of the block, and when using the short variable declaration, that shadows that variable.
This error can be reproduced with the following code:
var result []byte
customers := []int{}
w := os.Stdout
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
Attempting to compile and run it, we get the error (try it on the Go Playground):
prog.go:10:6: result declared and not used
Solution is to use a simple assignment instead of the short variable declaration if intention is to use the existing variable (in which case no new variable will be created), or use a different name for the variable if intention is not to use the outer, existing variable (but then the outer variable is to be removed or be used of course).
I have a device, which continues to send data over a serial port.
Now I want to read this and process it.
The data send this delimiter "!" and
as soon as this delimiter appears I want to pause reading to processing the data thats already been received.
How can I do that? Is there any documentation or examples that I can read or follow.
For reading data from a serial port you can find a few packages on Github, e.g. tarm/serial.
You can use this package to read data from your serial port. In order to read until a specific delimiter is reached, you can use something like:
config := &serial.Config{Name: "/dev/ttyUSB", Baud: 9600}
s, err := serial.OpenPort(config)
if err != nil {
// stops execution
log.Fatal(err)
}
// golang reader interface
r := bufio.NewReader(s)
// reads until delimiter is reached
data, err := r.ReadBytes('\x21')
if err != nil {
// stops execution
log.Fatal(err)
}
// or use fmt.Printf() with the right verb
// https://golang.org/pkg/fmt/#hdr-Printing
fmt.Println(data)
See also: Reading from serial port with while-loop
bufio's reader unfortunately did not work for me - it kept crashing after a while. This was a no-go since I needed a stable solution for a low-performance system.
My solution was to implement this suggestion with a small tweak. As noted, if you don't use bufio, the buffer gets overwritten every time you call
n, err := s.Read(buf0)
To fix this, append the bytes from buf0 to a second buffer, buf1:
if n > 0 {
buf1 = append(buf1, buf0[:n]...)
}
Then parse the bytes stored in buf1. If you find a subset you're looking for, process it further.
make sure to clear the buffers in a suitable manner
make sure to limit the frequency the loop is running with (e.g. time.Sleep)
I am new to Flatbuffers and GoLang. I am trying to implement a function that take converts an object to flatbuffer and retrieves the same object. Here is my code.
Updated Code
func getannouncements(){
annList := SR.GetFromDB().GetAllAnnouncementList()
fmt.Println(annList)
builder := flatbuffers.NewBuilder(1024)
var thisobjlist [12] flatbuffers.UOffsetT
for i,j := range annList{
annTitle := builder.CreateString(j.AnnTitle)
annText := builder.CreateString(j.AnnText)
annDate := builder.CreateString(j.AnnDate)
fb.AnnouncementStart(builder)
fb.AnnouncementAddAnnId(builder,int32(j.AnnID))
fb.AnnouncementAddAnnTitle(builder,annTitle)
fb.AnnouncementAddAnnText(builder, annText)
fb.AnnouncementAddAnnActive(builder,CR.BoolToByte(j.AnnActive))
fb.AnnouncementAddAnnDate(builder,annDate)
thisobj:= fb.AnnouncementEnd(builder)
thisobjlist[i] = thisobj
}
fb.AnnouncementListStartAnnListVector(builder,len(annList))
for _,j:=range thisobjlist{
builder.PlaceUOffsetT(j)
}
finalObj := fb.AnnouncementListEnd(builder)
builder.Finish(finalObj)
buf:= builder.FinishedBytes()
fmt.Println(buf)
/*bufItem := new(bytes.Buffer)
binary.Write(bufItem, binary.LittleEndian, buf)
buf1 := bufItem.Bytes()
buffyRead := bytes.NewReader(buf1)
var buffy []byte
binary.Read(buffyRead, binary.LittleEndian, &buffy)*/
Anncmt:= fb.GetRootAsAnnouncementList(buf,0)
anns := new(fb.Announcement)
if Anncmt.AnnList(anns,1){
thisLists := anns.AnnTitle()
fmt.Println(thisLists)
}
fmt.Println(Anncmt)
}
Schema File
namespace FlatBufs;
table Announcement{
AnnId:int;
AnnTitle:string;
AnnText:string;
AnnDate:string;
AnnActive:bool= false;
}
table AnnouncementList{
AnnName:string;
AnnList:[Announcement];
}
root_type AnnouncementList
The buf object is a byte array. However when I am generating the AnnGot obj I am still getting the almost same byte array as buf. So, When I read various posts on internet on this topic, I tried to convert that buf to binary type and then retrieve buf and tried to retrieve the data (as in the commented part of code). This time the object buffy doesn't have any data in it. I am still not clear what mistake I am making in this entire code.
Please point me in the right direction. Any help on this is much appreciated.
Thanks,
Tom
Not sure why you're using two builders. Each builder is creating one FlatBuffer, so things you store in one won't be available in the other.
A smaller problem is that you are nesting things (the Go implementation should guard against that, I believe?), i.e. you should finish building all announcements before you create a vector of them.. in your case this will all be mixed up.
You also appear to make your root an AnnouncementList, but then you try to access it as an Announcement.. this won't work. It be easier to see if we could see your schema.
I reading big file and sending this file by http POST.
I use bufio.
And now I want to modify one of first line of this file, how to do it ?
f := bufio.NewReaderSize(os.Stdin, 65536)
bufPart, err := f.Peek(65536))
//how to modify bufPart(f) ?
...
req, err := http.NewRequest("POST", url, f)
Two ideas how to do it:
Create your own Reader implementation that wraps an bufio.Reader and implements replacing logic (you will have to count number of read bytes).
Call io.Pipe, pass the returned PipeReader to NewRequest and start a separate goroutine that will read data from a file, modify it and write to the returned PipeWriter.
Hope this makes sense.
In the docs it is said that
If stored on disk, the File's underlying concrete type will be an
*os.File.
In this case everything is clear. Great. But, what happens if not, if the file is stored in memory?
My actual problem is that I´m trying to get the size of the different files stored in memory that I got though an html form but I can not use os.Stat to do fileInfo.Size() because I don´t have the location of the file, just it´s name.
fhs := req.MultipartForm.File["files"]
for _, fileHeader := range fhs {
file, _ := fileHeader.Open()
log.Println(len(file)) // Gives an error because is of type multipart.File
fileInfo, err := os.Stat(fileHeader.Filename) // Gives an error because it´s just the name, not the complete path
// Here I would do things with the file
}
You can exploit the fact that multipart.File implements io.Seeker to find its size.
cur, err := file.Seek(0, 1)
size, err := file.Seek(0, 2)
_, err := file.Seek(cur, 0)
The first line finds the file's current offset. The second seeks to the end of the file and returns where it is in relation to the beginning of the file. This is the size of the file. The third seeks to the offset we were at before trying to find the size.
You can read more about the seek method here.
if you call parseMultipartForm(0) this will write the entire file to disk instead of storing anything in memory, followed by f, _ := FormFile("file") then you can stat the file with fi, _ := f.(*os.File).Stat()
Depending on what you want to do with the data, the best thing to do may be to read the file into a byte slice with ioutil.ReadAll. (You might want the data as a byte slice eventually, anyway.) Once you've done that, you an find the length with len.