| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
Tags
- cli
- Matrix
- Refactoring
- reactivity
- websocket
- toggle
- todo-list
- graceful shutdown
- emit
- Dictionary
- method
- go
- Vue.js
- PROPS
- localStorage
- App.vue
- 행렬
- SFC
- URL
- goroutine
- golang
- TODO
- map
- Vue
- component
- container
- CDN
- channel
- Server
- goroutines
Archives
- Today
- Total
ksundev 님의 블로그
Dictionary 예제 - Update, Delete 본문
안녕하세요. 이번 포스트에서는 저번 포스트에 이어 Dictionary의 Update, Delete 메서드를 만들어봅시다.
# Update()
먼저 Search()를 통해 Update하려는 keyword가 있는지 확인해야 합니다. 그리고 적절한 에러처리를 해줍시다.
# mydict.go
var errCantUpdate = errors.New("can't update non-existing word")
// Update a word in the dictionary
func (d Dictionary) Update(word, def string) error {
_, err := d.Search(word)
switch err {
case errNotFound:
return errCantUpdate
case nil:
d[word] = def
return nil
}
return nil
}
이제 테스트코드를 작성해봅시다.
1. 먼저 없는 키워드를 업데이트 하려고 시도하면 에러가 발생해야 합니다.
2. 키워드와 새로운 def를 주면 def가 바뀌는지 테스트 합니다.
# main.go

# Delete()
삭제는 어떻게 해야할까요? 이건 삭제할 키워드만 입력받으면 될 것 같네요.
그리고 삭제는 Go의 내장함수를 사용합시다.
# mydict.go
var errCantDelete = errors.New("can't delete non-existing word")
// Delete a word from the dictionary
func (d Dictionary) Delete(word string) error {
_, err := d.Search(word)
switch err {
case errNotFound:
return errCantDelete
case nil:
delete(d, word)
return nil
}
return nil
}
이제 테스트 코드를 작성해봅시다.
# main.go

예상한대로 잘 작동하네요.
Dictionary 예제는 여기까지입니다!
다음 포스트부터는 URL Checker와 Go Routines 예제를 만들어보겠습니다 :)
'[개발] Go > 초급 프로젝트' 카테고리의 다른 글
| Dictionary 예제 - Search, Add (0) | 2025.06.25 |
|---|---|
| [예제] Bank Project - 3 (0) | 2025.06.14 |
| [예제] Bank Project - 2 (Method) (0) | 2025.06.10 |
| [예제] Bank Project - 1 (0) | 2025.06.10 |