본문 바로가기

Go4

Go - 슬라이스 사용법 ( cap, make, slicing, copy ) 1. 슬라이스 초기화 var x = []int{1, 2, 3, 4, 5}| 슬라이스는 [], 배열은 [...] 2. nil 로만 비교 가능 var x2 = []int{1, 2, 3, 4, 5} fmt.Println(x == x2) // invalid operation: x == x2 (slice can only be compared to nil)var empty []int fmt.Println(empty) fmt.Println(empty == nil) // slice는 오직 nil 로만 비교 가능3. append var a []int fmt.Println("a length:", len(a)) a = append(a, 5) fmt.Println("a length:", len(a), a) a = append.. 2022. 10. 6.
Go - 배열 초기화 및 사용 방법 1. Array 초기화 var x [10]int // 제로값으로 초기화 fmt.Println("x의 값은?", x)x의 값은? [0 0 0 0 0 0 0 0 0 0]2. 리터럴을 사용한 초기화 var fruits = [3]string{"apple", "banana", "kiwi"} // 배열 리터럴 사용 fmt.Println("fruits의 값은?", fruits) fruits의 값은? [apple banana kiwi]3. 비교 var x1 = [3]int{1, 2, 3} var x2 = [...]int{1, 2, 3} fmt.Println(x1 == x2) // true4. 배열 크기 확인 fmt.Println(len(x1)) // 35. 값 할당 위 x1 array 변수에 값을 할당한다. x1[0].. 2022. 9. 28.
Go - 문제해결: Error loading workspace: gopls was not able to find modules in your workspace. VSCode에서 go 파일을 열면 다음과 같은 오류와 함께 package main 에 빨간 줄이 생겼다. (해당 오류가 발생해도 go run 은 문제없이 동작한다.) VSCode 하단에 다음과 같은 알림을 확인하였다. Error loading workspace: gopls was not able to find modules in your workspace. When outside of GOPATH, gopls needs to know which modules you are working on. You can fix this by opening your workspace to a folder inside a Go module, or by using a go.work file to specify multip.. 2022. 9. 27.
Go - 숫자(int)를 문자열(string)로 변환 ( 함수 별 벤치마킹 ) int -> string 변환 방법 1. strconv.Itoa package main import ( "fmt" "strconv" ) func main() { var fruits [3]string fmt.Println(fruits, len(fruits)) for i := 0; i < len(fruits); i++ { fruits[i] = strconv.Itoa(i) fmt.Println(i, fruits[i]) } } 다른 변환 함수에 비해 제일 나은 성능을 보인다. 2. strconv.FormatInt for i := 0; i < len(fruits); i++ { fruits[i] = strconv.FormatInt(int64(i), 10) fmt.Println(i, fruits[i]) } 두 번째 .. 2022. 9. 13.