sprint() 는 문자열을 합쳐주는 함수이다.
밑의 예시를 보면
meditation := fmt.Sprintln(step1, step2)에서
둘의 문자열이 합쳐진 값이 meditation에 저장되고 이를 print로 출력하는 모습을 볼 수 있다.
그리고 ln넣고 안넣고 차이는 띄어쓰기이다. 넣으면 각 문자열 사이에 띄어쓰기가 된 상태로 저장이 된다.
package main
import "fmt"
func main() {
step1 := "Breathe in..."
step2 := "Breathe out..."
// Add your code below:
meditation := fmt.Sprintln(step1, step2)
fmt.Print(meditation)
}
RUN
Breathe in... Breathe out...
이 함수는 좀 특별하다. printf를 문자열의 조합으로 사용할 수 있다.
밑의 예시로 해석하자면,
wish := fmt.Sprintf(template, pet) 은
fmt.printf("I wish I had a %v.", pet)과 같이 작용하는 것이다.
일반 printf처럼 함수를 쓰되 이를 변수로 대체해도 되는 것이다.
package main
import "fmt"
func main() {
template := "I wish I had a %v."
pet := "puppy"
// Add your code below:
wish := fmt.Sprintf(template, pet)
fmt.Println(wish)
}
RUN
I wish I had a puppy.