Loading the catalog…
Loading the catalog…
오늘은 좀 더 다양한 go 의 사용법을 알아보자 일단 결론 부터 서술 하겠다. go의 반복문안 switch 문 case 안에서 break 를 하면 반복문을 탈출하는게 아니라 switch문을 탈출한다. go의 Atoi 는 공백을 처리 못한다. go의 구조체는 캡슐화가 가능하며 구조체를 정의하는 시작문자의 대소문자로 판별한다. 대문자로 시작 (Exported): 다른 외부 패키지에서 접근 가능 (public 역할) 소문자로 시작 (Unexported): 동일한 패키지 내부에서만 접근 가능 (private 역할) slice 에서 len 랑 range 의 차이점은 len은 길이를 구하는 내장 함수이고, range는 슬라이스를 순회(반복)할 때 사용하는 키워드 이다. if 문에서 초기화를 하고 바로 조건 검사를 할 수 있다. if i := strings.TrimSpace; i == "Hello"{<code>} 이런 식으로 가능하다. 아직까지 큰 문제는 go 의 자료구조와 에러처리 방법 문법에 대해서 어려움을 가지고 있다는 것이다. 특히 문법을 틀려서 error 가 나는 상황에서 수정을 하는데 너무 괴롭다. 문제를 찾기위해 문법 검사기가 알려주는 error의 정의에 대해서 찾고 그걸 다시 내코드에 적용하는 과정이 힘들다. 코드 package main import ( "bufio" "errors" "fmt" "os" "strconv" "strings" ) type Task struct { ID int Title string Done bool } type Store struct { Tasks []Task NextID int } func (s *Store) Add(title string) (Task, error) { task := strings.TrimSpace(title) if task == "" { return Task{}, errors.New("title is empty") } s.NextID++ tmp := Task{ ID: s.NextID, Title: task, Done: false, } s.Tasks = append(s.Tasks, tmp) return tmp, nil } func (s *Store) Complete(id int) error { for i := 0; i < len(s.Tasks); i++ { if s.Tasks[i].ID == id { s.Tasks[i].Done = true fmt.Println(s.Tasks[i]) return nil } } return errors.New("No match ID check again") } func split_token(scan *bufio.Scanner) ([]string, error) { if scan.Scan() { text := scan.Text() reValue := strings.TrimSpace(text) for i := 0; i < len(reValue); i++ { b := reValue[i] if isASCIIAlpha(b) == false { return []string{reValue[:i], reValue[i:]}, nil } } return []string{reValue}, nil } return nil, errors.New("Scanning Error Checkagain") } func (s *Store) Done(str string) error { number, err := strconv.Atoi(strings.TrimSpace(str)) if err != nil { return errors.New("AtoiError") } for i := range s.Tasks { if s.Tasks[i].ID == number { s.Tasks[i].Done = true return nil } } return errors.New("not found ID") } func isASCIIAlpha(b byte) bool { return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') } func newStore() *Store { return &Store{NextID: 0} } func (s *Store) print_list() { for _, task := range s.Tasks { fmt.Println(task) } } func main() { store := newStore() Scanner := bufio.NewScanner(os.Stdin) for { if text, err := split_token(Scanner); err == nil { switch text[0] { case "add": if len(text) == 2 { store.Add(text[1]) } else { fmt.Println("add command is : add <title>") } case "list": store.print_list() case "done": if len(text) == 2 { if err := store.Done(text[1]); err != nil { fmt.Println(err) } } else { fmt.Println("add command is : add <title>") } case "exit": fmt.Println("good bye sir") return case "": continue default: fmt.Println("Command type is ADD, list , exit") } } else { fmt.Println(err) } } }
What RADAR observed and classified to build this opportunity. It is what the source published, not a verification that the offer is still active.
go를 배워보자(3). 오늘은 좀 더 다양한 go 의 사용법을 알아보자 일단 결론 부터 서술 하겠다. go의 반복문안 switch 문 case 안에서 break 를 하면 반복문을 탈출하는게 아니라 switch문을 탈출한다. go의 Atoi 는 공백을 처리 못한다. go의 구조체는 캡슐화가 가능하며 구조체를 정의하는 시작문자의 대소문자로 판별한다. 대문자로 시작 (Exported): 다른 외부 패키지에서 접근 가능 (public 역할) 소문자로 시작 (Unexported): 동일한 패키지 내부에서만 접근 가능 (private 역할) slice 에서 len 랑 range 의 차이점은 len은 길이를 구하는 내장 함수이고, range는 슬라이스를 순회(반복)할 때 사용하는 키워드 이다. if 문에서 초기화를…
Open source