IT/Go 언어 (Golang)
[Golang] 문자열과 문자열의 종류
공대생의 차고
2019. 12. 22. 21:50
반응형
- Golang에서는 char 자료형이 없다.
- 대신 string 자료형이 굉장히 유연하고 유용하게 사용된다.
- string 자료형은 크게 2 종류로 나뉜다.
- Raw String Literal: Back Quote(` `)로 둘러싸인 string. '\n' 등을 NewLine으로 해석하지 않고 있는 그대로 표기한다. 즉, 여러 줄의 문자를 치기 좋다.
- 주의할 점: 작은 따옴표(' ')가 아닌, shift와 같이 누르면 물결표(~)가 나오는 문자인 Back Quote(` `)를 사용해야한다!
- Interpreted String Literal: 큰 따옴표(" ")로 둘러싸인 string. 복수라인으로 선언할 수 없으며 연산자들을 제대로 반영한다. C/C++의 String과 비슷하게 사용 가능하다.
- Raw String Literal: Back Quote(` `)로 둘러싸인 string. '\n' 등을 NewLine으로 해석하지 않고 있는 그대로 표기한다. 즉, 여러 줄의 문자를 치기 좋다.
- Raw String Literal
//Raw String Literal
package main
import "fmt"
func main() {
s := 'This
is
Sparta!!!\n'
fmt.Println(s)
}
- 결과는 아래와 같다.
This
is
Sparta!!!\n
- Interpreted String Literal
//Interpreted String Literal
package main
import "fmt"
func main() {
s := "This\nis\nSparta!!!"
fmt.Println(s)
}
- 결과는 아래와 같다.
This
is
Sparta!!!
반응형