Go 문자열 출력 행번호 붙이기

Jmnote (토론 | 기여)님의 2024년 7월 7일 (일) 11:37 판 (Jmnote님이 Go Println 행번호 붙이기 문서를 Go 문자열 출력 행번호 붙이기 문서로 이동했습니다)

1 개요

Go 문자열 출력 행번호 붙이기
Go 여러줄 문자열 행번호 붙여 출력
package main

import (
	"bufio"
	"fmt"
	"strings"
)

func main() {
	a := `Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.`
	catNumber(a)
}

func catNumber(input string) {
	scanner := bufio.NewScanner(strings.NewReader(input))
	line := 1
	for scanner.Scan() {
		fmt.Printf("%d: %s\n", line, scanner.Text())
		line++
	}
	if err := scanner.Err(); err != nil {
		fmt.Println("Error reading string:", err)
	}
}

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}