Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- ㅒ
- 선형대수
- 데비안
- /etc/network/interfaces
- 이터레이터
- resttemplate
- d
- 네트워크 설정
- baeldung
- 스프링부트핵심가이드
- 티스토리 쿠키 삭제
- 코드로배우는스프링웹프로젝트
- 친절한SQL튜닝
- Kernighan의 C언어 프로그래밍
- iterator
- 리눅스
- 스프링 시큐리티
- 코드로배우는스프링부트웹프로젝트
- GIT
- 처음 만나는 AI 수학 with Python
- 알파회계
- 자바편
- 페이징
- 구멍가게코딩단
- 자료구조와 함께 배우는 알고리즘 입문
- network configuration
- 처음 만나는 AI수학 with Python
- 서버설정
- 목록처리
- 자료구조와함께배우는알고리즘입문
Archives
- Today
- Total
bright jazz music
Go - map 타입에 메소드 추가하기(Update, Delete) 본문
package myDict
import "errors"
// map은 struct가 아니다. array처럼 타입이다.
// type에도 메소드를 추가할 수 있다. struc에만 추가할 수 있는 것이 아니다.
// type 옆의 Dictionary는 map[string]string의 alias 같은 것임.
//map은 자동으로 *를 사용한다.
//Dictionary Type
type Dictionary map[string]string
var (
errNotFound = errors.New("Not Found")
errWordExists = errors.New("That word already exists")
errCantUpdate = errors.New("Cant update non-existing word")
)
// Search for a word
func (d Dictionary) Search(word string) (string, error) {
//Dictionary라는 이름의 map에 Search 메소드를 더하는 것이다.
//이 메소드는 string 타입을 word에 받고, string 또는 error로 반환한다.
value, exists := d[word]
if exists {
return value, nil // 값이 없는 경우 nil 반환
}
return "", errNotFound
}
//Add a word to the dictionary
func (d Dictionary) Add(word, def string) error { //추가하려는 단어가 없으면 에러
_, err := d.Search(word) //definition은 필요 없어서 이그노어
switch err {
case errNotFound:
d[word] = def
case nil:
return errWordExists
}
return nil
//if문으로 표현한다면?
// if err == errNotFound{ //추가하려는 단어가 없다면
// d[word] = def //d[키] = 값
// } else if err == nil{ // 그 단어가 이미 있다면
// return errWordExists
// }
// return nil
}
//Update a word
func (d Dictionary) Update(word, definition string) error {
_, err := d.Search(word)
switch err {
case nil: //그 단어가 있다면
d[word] = definition //새로 정의
case errNotFound:
return errCantUpdate
}
return nil
}
메인
package main
import (
"fmt"
"github.com/hojuncha997/learngo/accounts/myDict"
)
func main() {
dictionary := myDict.Dictionary{}
baseWord := "hello"
dictionary.Add(baseWord, "First") //hello: First 입력
err := dictionary.Update(baseWord, "Second") //hello의 value를 second로 업데이트
if err != nil {
fmt.Println(err)
}
word, _ := dictionary.Search(baseWord)
fmt.Println(word)
}
====
Second
Delete 메소드 만들기
package myDict
import "errors"
// map은 struct가 아니다. array처럼 타입이다.
// type에도 메소드를 추가할 수 있다. struc에만 추가할 수 있는 것이 아니다.
// type 옆의 Dictionary는 map[string]string의 alias 같은 것임.
//map은 자동으로 *를 사용한다.
//Dictionary Type
type Dictionary map[string]string
var (
errNotFound = errors.New("Not Found")
errWordExists = errors.New("That word already exists")
errCantUpdate = errors.New("Cant update non-existing word")
)
// Search for a word
func (d Dictionary) Search(word string) (string, error) {
//Dictionary라는 이름의 map에 Search 메소드를 더하는 것이다.
//이 메소드는 string 타입을 word에 받고, string 또는 error로 반환한다.
value, exists := d[word]
if exists {
return value, nil // 값이 없는 경우 nil 반환
}
return "", errNotFound
}
//Add a word to the dictionary
func (d Dictionary) Add(word, def string) error { //추가하려는 단어가 없으면 에러
_, err := d.Search(word) //definition은 필요 없어서 이그노어
switch err {
case errNotFound:
d[word] = def
case nil:
return errWordExists
}
return nil
//if문으로 표현한다면?
// if err == errNotFound{ //추가하려는 단어가 없다면
// d[word] = def //d[키] = 값
// } else if err == nil{ // 그 단어가 이미 있다면
// return errWordExists
// }
// return nil
}
//Update a word
func (d Dictionary) Update(word, definition string) error {
_, err := d.Search(word)
switch err {
case nil: //그 단어가 있다면
d[word] = definition //새로 정의
case errNotFound:
return errCantUpdate
}
return nil
}
//Delete aword
func (d Dictionary) Delete(word string) { //delete(m, "route")
delete(d, word)
}
메인
package main
import (
"fmt"
"github.com/hojuncha997/learngo/accounts/myDict"
)
func main() {
dictionary := myDict.Dictionary{}
baseWord := "hello"
dictionary.Add(baseWord, "First")
dictionary.Search(baseWord)
dictionary.Delete(baseWord)
word, err := dictionary.Search(baseWord)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(word)
}
}
===
Not Found
'Language > Go' 카테고리의 다른 글
GO - Goroutines 고루틴 기본 (0) | 2021.02.19 |
---|---|
Go - URL Checker 하나 씩 순차적 (0) | 2021.02.19 |
Go- map 타입에 메소드 추가하기(Add) (0) | 2021.02.19 |
Go -map type에 메소드 추가하기(Search) (0) | 2021.02.19 |
Go - 메소드2 (Go의 메소드 내부적 호출/ struct에 메소드 추가하기) (0) | 2021.02.18 |
Comments