package main import ( "fmt" ) func main() { a := -4 b := 7 flag := true fmt.Print(solution(a, b, flag)) } func solution(a int, b int, flag bool) int { if flag == true { return a + b } else { return a - b } }
package main import ( "fmt" "strconv" ) func main() { a := 2 b := 91 fmt.Print(solution(a, b)) } func solution(a int, b int) int { A, _ := strconv.Atoi(strconv.Itoa(a) + strconv.Itoa(b)) B := 2 * a * b if A >= B { return A } else { return B } }
package main import ( "fmt" "strconv" ) func main() { a := 9 b := 91 fmt.Print(solution(a, b)) } func solution(a int, b int) int { A, _ := strconv.Atoi(strconv.Itoa(a) + strconv.Itoa(b)) B, _ := strconv.Atoi(strconv.Itoa(b) + strconv.Itoa(a)) if A > B { return A } else { return B } }
데이터 타입 변환을 손쉽게 다룰 수 있게 해주는 'strconv' 패키지에 대해서 정리해봤다. 'strconv' 패키지는 문자열과 숫자 간의 변환을 지원한다. 'Atoi' 함수는 문자열을 정수로, 'Itoa'함수는 정수를 문자열로 변환하는데 사용된다. Atoi(String to Int) str := "123" num, err := strconv.Atoi(str) if err != nil { fmt.Println(err) } else { fmt.Println(num) // 출력: 123 } Itoa (int to String) num := 123 str := strconv.Itoa(num) fmt.Println(str) // 출력: "123" Parse 함수 'strconv' 패키지는 'Parse' 함수를..