목록Programing (20)
Study Memory Work
파일생성 : Create package : os 새 파일 작성 및 파일 열기 파일 관련 메소드에는 예외처리를 확실히 해줘야 한다. 파일을 오픈하고 나면 꼭 닫아주어야한다. package main import ( "fmt" "os" ) func errCheck1(e error) { if e != nil { panic(e) } } func main() { file, err := os.Create("fileIO/anyfile.txt") errCheck1(err)// 예외처리 중요! // 파일 열고난 후에는 close 필수!! defer file.Close() } 파일 쓰기 : write, writeString, writeAt package : os // 예제 1 : Write s1 := []byte{123, ..
Document 예제로 배우는 Go 프로그래밍 - Go 에러처리 1. Go 에러 Go는 내장 타입으로 error 라는 interface 타입을 갖는다. Go 에러는 이 error 인터페이스를 통해서 주고 받게 되는데, 이 interface는 다음과 같은 하나의 메서드를 갖는다. 개발자는 이 인터페 golang.site 기본 에러처리 fmt.Errorf 를 return. 잘 쓰지 않는 방법..! func notZero(n int) (string, error) { if n != 0 { s := fmt.Sprint("Hello Golang : ", n) return s, nil } return "", fmt.Errorf("%d를 입력했습니다. 에러 발생!", n) } func main() { // error ..
package main import ( "fmt" "time" ) func main() { p := fmt.Println now := time.Now() p(now)// 2012-10-31 15:50:13.793654 +0000 UTC then := time.Date( 2009, 11, 17, 20, 34, 58, 651387237, time.UTC) p(then)// 2009-11-17 20:34:58.651387237 +0000 UTC p(then.Year()) p(then.Month()) p(then.Day()) p(then.Hour()) p(then.Minute()) p(then.Second()) p(then.Nanosecond()) p(then.Location()) p(then.Weekday()..
package main import ( "fmt" "os" ) type point struct { x, y int } func main() { p := point{1, 2} fmt.Printf("struct1: %v\n", p)// struct1: {1 2} fmt.Printf("struct2: %+v\n", p)// struct2: {x:1 y:2} fmt.Printf("struct3: %#v\n", p)// struct3: main.point{x:1, y:2} fmt.Printf("type: %T\n", p)// type: main.point fmt.Printf("bool: %t\n", true)// bool: true fmt.Printf("int: %d\n", 123)// int: 123 fmt.P..
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { p("Contains: ", s.Contains("test", "es")) p("Count: ", s.Count("test", "t")) p("HasPrefix: ", s.HasPrefix("test", "te")) p("HasSuffix: ", s.HasSuffix("test", "st")) p("Index: ", s.Index("test", "e")) p("Join: ", s.Join([]string{"a", "b"}, "-")) p("Repeat: ", s.Repeat("a", 5)) p("Replace: ", s.Replace("foo", "o", "0", -1)..
Hello World Values Variables Constants For If/Else Switch Arrays Slices Maps Range Functions Multiple Return Values Variadic Functions Closures Recursion Pointers Strings and Runes Structs Methods Interfaces Struct Embedding Generics Errors Goroutines Channels Channel Buffering Channel Synchronization Channel Directions Select Timeouts Non-Blocking Channel Operations Closing Channels Range over ..