I thought that after getting the bytes, chunk the data, I need to have the io.writer back before send the chunked data So far I came up with this:
... cmd.Stdin = conn buf := new(bytes.Buffer) cmd.Stdout = buf bizz := buf.Bytes() var i int for { n := int(math.Min(float64(rand.Intn(len(bizz))), float64(len(bizz)))) d := bizz[i : i+n] i += n z := bytes.NewBuffer(d) kk := io.Writer(z) //this line creates the error time.Sleep(400 * time.Millisecond) kk = conn if i >= len(bizz) { break } } cmd.Stderr = conn cmd.Run() ..... But know I get this error: conn.go:95:21: kk declared but not used Il giorno mercoledì 7 luglio 2021 alle 15:49:56 UTC+2 mlevi...@gmail.com ha scritto: > You cannot *per se* convert an interface or a struct to a builtin like > []byte > You might wanna have a look at the Bytes method > <https://golang.org/pkg/bytes/#Buffer.Bytes> of *bytes.Buffer, which > returns the internal buffer of the type as a slice of bytes. Normally that > would have been a good exercise to let you find it yourself, and I don't > know if it is really "help" to give it to you directly, but as I said, once > you are done with your small tool, the next step for you will be to go back > from the basic Go constructs :) > > Glad I could help, and don't bother with the comments, the best "thank > you" I can wish for is that we continue learning together ;) > Hope the following adventures of your Go journey are as interesting as > they are for me! > > Cheers > > Le mer. 7 juil. 2021 à 15:08, LetGo <non3...@gmail.com> a écrit : > >> Thanks for your answer!(: >> You are right, but I just wanted to have this one little tool in Go and >> I have never thought that could be that hard... ahahah >> >> By the way, it works as you said, it fixed the error! ( obviously.. ) >> the only thing left is to convert type *bytes.Buffer to []byte * I think* >> and then I will be almost done. >> Im already searching how to do that. >> >> Once it will work as I wish, I will add your names to my comments ( I >> think this is better than any "thank you" ) in the code, to remind me of >> your kind help(: >> >> >> >> Il giorno mercoledì 7 luglio 2021 alle 14:01:33 UTC+2 mlevi...@gmail.com >> ha scritto: >> >>> [Sorry forgot to hit "Reply all"] >>> >>> Are you trying to cast cmd.Stdout here? >>> What you can do is: >>> buf := new(bytes.Buffer) >>> cmd.Stdout = buf // buf is an io.Writer so this is fine >>> >>> but I don't get the point of the data := foo? >>> >>> Maybe, before trying to convert a whole complex program in Python to a >>> whole working program in Go, you should take time to familiarize yourself >>> with the language. >>> Go through the Go tour <https://tour.golang.org/welcome/1>, read a >>> little of the specs, have yourself code some small, simple programs that >>> don't require using lots of std packages at once... >>> >>> Once you are familiar with the language constructs, which I have to say >>> are pretty different from Python's, you will have a better understanding of >>> where to start and how to implement your program. Otherwise I think this >>> will all only get you confused. >>> And understanding at least the important basics of Go will help you >>> explain your pain points here, if any remains :) >>> >>> Hope this helps, >>> >>> Le mer. 7 juil. 2021 à 12:41, LetGo <non3...@gmail.com> a écrit : >>> >>>> One of these is this: >>>> ... >>>> buf := new(bytes.Buffer) >>>> foo := buf(cmd.Stdout) // this line is 87 >>>> data := foo >>>> var i int >>>> ... >>>> >>>> pkg/conn.go:87:20: cannot call non-function buf (type *bytes.Buffer) >>>> Il giorno mercoledì 7 luglio 2021 alle 12:10:03 UTC+2 LetGo ha scritto: >>>> >>>>> I tried also both of them, but I got stuck into a loop of errors >>>>> again.. probably I coded in the wrong way >>>>> >>>>> Il giorno mercoledì 7 luglio 2021 alle 11:50:51 UTC+2 Brian Candler ha >>>>> scritto: >>>>> >>>>>> It makes no sense to convert an io.Writer to a string. >>>>>> >>>>>> io.Writer is an interface: any type which has a Write() method. So >>>>>> you can pass a string *to* a writer, to get it written somewhere, by >>>>>> calling the Write() method. In general, you can't get a string *from* a >>>>>> writer. If you google "go io.Writer" you'll get lots of tutorials and >>>>>> examples. >>>>>> >>>>>> Depending on your application though, you might want to create a >>>>>> bytes.Buffer <https://golang.org/pkg/bytes/#Buffer> or >>>>>> strings.Builder <https://golang.org/pkg/strings/#Builder> object, >>>>>> both of which are an io.Writer. The written data gets appended to a >>>>>> buffer >>>>>> that you can read later. >>>>>> >>>>>> On Wednesday, 7 July 2021 at 10:07:19 UTC+1 LetGo wrote: >>>>>> >>>>>>> Thanks for your answer!(: >>>>>>> You are right, sorry! >>>>>>> This is the code: https://play.golang.org/p/zEZ2HIUNffs >>>>>>> >>>>>>> About the lines, wow! Yes, you got them! ahah >>>>>>> About the errors, I tried to convert ( cmd.Stdout ) io.Write to >>>>>>> bytes/ strings, but.. I have then entered into a loop of errors... >>>>>>> >>>>>>> >>>>>>> >>>>>>> Il giorno martedì 6 luglio 2021 alle 21:32:10 UTC+2 Brian Candler ha >>>>>>> scritto: >>>>>>> >>>>>>>> You haven't shown which lines 75, 76 and 83 correspond to. It's >>>>>>>> easier if you put the whole code on play.golang.org, and we'll be >>>>>>>> able to point to the error. >>>>>>>> >>>>>>>> But I'm guessing it's this: >>>>>>>> data := cmd.Stdout >>>>>>>> ... >>>>>>>> n := int(math.Min(float64(rand.Intn(len(data))), >>>>>>>> float64(len(data)))) << line 75? >>>>>>>> d := data[i : i+n] << line 76? >>>>>>>> ... >>>>>>>> if i >= len(data) { << line 83? >>>>>>>> >>>>>>>> If I'm right, the compiler is saying: cmd.Stdout (which you >>>>>>>> assigned to 'data') is of type io.Writer. It's not a string; you >>>>>>>> can't >>>>>>>> take len(...) of an io.Writer, nor can you slice it. >>>>>>>> >>>>>>>> On Tuesday, 6 July 2021 at 16:03:26 UTC+1 LetGo wrote: >>>>>>>> >>>>>>>>> I think I made some progress.... I think. Is it right what I'm >>>>>>>>> doing ? >>>>>>>>> >>>>>>>>> ................ >>>>>>>>> cmd.Stdin = conn >>>>>>>>> // cmd.Stdout = conn >>>>>>>>> // data := []byte(cmd.Stdout) >>>>>>>>> data := cmd.Stdout >>>>>>>>> var i int >>>>>>>>> for { >>>>>>>>> n := int(math.Min(float64(rand.Intn(len(data))), >>>>>>>>> float64(len(data)))) >>>>>>>>> d := data[i : i+n] >>>>>>>>> i += n >>>>>>>>> time.Sleep(400 * time.Millisecond) >>>>>>>>> d = conn >>>>>>>>> >>>>>>>>> if i >= len(data) { >>>>>>>>> break >>>>>>>>> } >>>>>>>>> } >>>>>>>>> cmd.Stderr = conn >>>>>>>>> cmd.Run() >>>>>>>>> ............................ >>>>>>>>> >>>>>>>>> But when I try to build I get these errors: >>>>>>>>> >>>>>>>>> conn.go:75:46: invalid argument data (type io.Writer) for len >>>>>>>>> conn.go:76:16: cannot slice data (type io.Writer) >>>>>>>>> conn.go:83:22: invalid argument data (type io.Writer) for len >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> Il giorno martedì 29 giugno 2021 alle 19:37:04 UTC+2 LetGo ha >>>>>>>>> scritto: >>>>>>>>> >>>>>>>>>> Thank you guys for all your answers and suggestions! >>>>>>>>>> I really appreciate! >>>>>>>>>> Sorry about the screenshots, it was the only way to make the >>>>>>>>>> packets "human readable" >>>>>>>>>> How could you code that kind of implementation based on your >>>>>>>>>> knowledge and skill? >>>>>>>>>> I have noone of these in golang ahah as I said, im too newbie to >>>>>>>>>> do all this alone! >>>>>>>>>> Also not working examples ( if they throw an error I don't care, >>>>>>>>>> based on my code are fine! >>>>>>>>>> These examples could rapresent a great start from me!(: >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> Il giorno martedì 29 giugno 2021 alle 19:00:06 UTC+2 >>>>>>>>>> jesper.lou...@gmail.com ha scritto: >>>>>>>>>> >>>>>>>>>>> On Tue, Jun 29, 2021 at 5:24 PM LetGo <non3...@gmail.com> wrote: >>>>>>>>>>> >>>>>>>>>>>> Thanks for the answer! (: >>>>>>>>>>>> In python it was straightforward to implement and it works like >>>>>>>>>>>> a charm. It sends small packets with delay between each other >>>>>>>>>>>> without even >>>>>>>>>>>> care if it is UDP or TCP: >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>> Beware! This is an assumption that will break at some point in >>>>>>>>>>> time. Currently the delay and the OS makes things straightforward >>>>>>>>>>> for you. >>>>>>>>>>> But TCP doesn't behave like you expect, and you are very likely to >>>>>>>>>>> run into >>>>>>>>>>> trouble if the machine, the network, or the system starts taking >>>>>>>>>>> additional >>>>>>>>>>> load. >>>>>>>>>>> >>>>>>>>>>> You need to frame the data. A good way is to use 4 bytes as a >>>>>>>>>>> size (unsigned 32 bit integer), followed by a payload of that size. >>>>>>>>>>> You can >>>>>>>>>>> then avoid this becoming an uncontrolled explosion in your software >>>>>>>>>>> at a >>>>>>>>>>> later date. You can also close connections early if too large >>>>>>>>>>> messages get >>>>>>>>>>> sent, etc. >>>>>>>>>>> >>>>>>>>>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "golang-nuts" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to golang-nuts...@googlegroups.com. >>>> >>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/golang-nuts/466d016b-b90b-4505-b4a8-7e5fc62679b4n%40googlegroups.com >>>> >>>> <https://groups.google.com/d/msgid/golang-nuts/466d016b-b90b-4505-b4a8-7e5fc62679b4n%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/0f12076e-f325-4583-ba85-857084aec83dn%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/0f12076e-f325-4583-ba85-857084aec83dn%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2aebe5af-a078-4165-bde3-e235845c3de1n%40googlegroups.com.