Skip to content

Reading a line of text from file #252

Answered by mna
dnbsd asked this question in Q&A
Aug 7, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

It seems that os.stdin.read() without argument will read until the EOF, so it stops only after ctrl-D. This program seems to do what you want, passing a byte slice to read so that it fills it until a newline is found:

buf := byte_slice(10)

line := ""
for {
	printf("> ")

	line += string(os.stdin.read(buf))
	for {
		// interestingly, for !strings.contains(line, "\n") {...} does not work
		if strings.contains(line, "\n") {
			break
		} else {
			line += string(os.stdin.read(buf))
		}
	}
	parts := strings.split(line, "\n")
	cmd := switch len(parts) {
		case 0:
			line = ""
			""
		case 1:
			line = ""
			parts[0]
		case 2:
			line = parts[1]
			parts[0]
	}

	switch cmd {
	case "echo":
		pri…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@dnbsd
Comment options

@mna
Comment options

Answer selected by dnbsd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants