Thursday, December 5, 2024
HomeGolangI would prefer to learn longer textual content utilizing Golang stdin -...

I would prefer to learn longer textual content utilizing Golang stdin – Getting Assist


I need to enter and browse a string of greater than 10000 characters utilizing Golang’s stdin module.

Nonetheless, with my present data of Golang, I can solely learn first 4096 characters.

So I wish to anybody to assist me for fixing of this drawback.

Thanks.

What’s your present strategy? Truly, there must be no boundary on how a lot you’ll be able to learn on stdin.

It must be attainable to learn os.Stdin utilizing os.ReadAll.

bundle most important

import (
	"io"
	"os"
)

func most important() {
	res, err := io.ReadAll(os.Stdin)
	if err != nil {
		panic(err)
	}

	println(len(res))
}

It reads Stdin right into a buffer and appeds it to a consequence buffer till an error happens. If the error is io.EOF, it returns the collected buffer. In any other case, the error is returned. It really works a bit like the next snippet.

bundle most important

import (
	"io"
	"os"
)

func most important() {
	buf := make([]byte, 1024)
	var res []byte

	for {
		n, err := os.Stdin.Learn(buf)
		res = append(res, buf[:n]...)
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}
	}

	println(len(res))
}

I hope that was considerably useful.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments