"Go 문자열 출력 행번호 붙이기"의 두 판 사이의 차이

(새 문서: ==개요== ;Go Println 행번호 붙이기 <syntaxhighlight lang='go' run> package main import ( "bufio" "fmt" "strings" ) func main() { a := `Lorem ipsum dolor sit amet, conse...)
 
1번째 줄: 1번째 줄:
==개요==
==개요==
;Go Println 행번호 붙이기
;Go 문자열 출력 행번호 붙이기
;Go 여러줄 문자열 행번호 붙여 출력


<syntaxhighlight lang='go' run>
<syntaxhighlight lang='go' run>

2024년 7월 7일 (일) 11:37 판

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 }}