728x90

2024/11 4

[GO] 에러 핸들링 방법

고랭에서 에러 핸들링을 하기 위해서는 먼저 "errors" 라는 패키지를 import 해야한다.지난 번에 작성한 greetings.go에 errors를 import 해보자 package greetingsimport ( "errors" "fmt") 그 다음 우리는 다음과 같은 상황에서 에러를 뱉을 것이다.[이름 변수에 빈 값이 들어올 경우] if name == "" { return "", errors.New("empty name")} 그리고 이 코드를 가지고 있는 함수는 error를 뱉을 수 있기 때문에 함수를 다음과 같이 작성한다.// Hello returns a greeting for the named person.func Hello(name string) (string, error) ..

[GO] 로컬 패키지 호출 방법

지난번 포스팅에는 로컬에 패키지를 만드는 법을 알아보았다.이번엔 로컬에 만들어둔 패키지를 다른 패키지에서 어떻게 호출해서 사용하는지 확인 해볼 예정이다. 우선 두 개의 패키지가 필요하다.-root |- hello |- greetings 이중에서 greetings는 호출 당할 패키지, hello는 greetings를 호출할 패키지 이다. 우선 greetings 패키지 안에 greetings.go를 만든다.코드는 다음과 같다. package greetingsimport "fmt"func Hello(name string) string { message := fmt.Sprintf("Hi, %v. Welcome!", name) return message} 다음으로 hello 패키지를 만들고 초기화 한 다음해당..

[GO] 함수 기본 작성법

비단 go 언어 뿐만 아니라 모든 프로그래밍 언어에서 제일 먼저 입력해보는 것은 "hello world" 일 것이다. 그래서 go를 시작했기 때문에 go로도 hello world를 작성해보고자 한다. 우선 go파일을 만들고 함수를 하나 만들 것이다.package greetingsimport "fmt"// Hello returns a greeting for the named person.func Hello(name string) string { // Return a greeting that embeds the name in a message. message := fmt.Sprintf("Hi, %v. Welcome!", name) return message}(위의 코드는 나의 사랑 고랭 공식..

[GO] 모듈과 패키지

https://go.dev/learn/  Get Started - The Go Programming LanguageGetting started In this tutorial, you'll get a brief introduction to Go programming. Along the way, you will install Go, write some simple "Hello, world" code, use the go command to run your code, use the Go package discovery tool, and call functions of ango.dev위의 페이지는 고랭 공식에서 지원하는 학습 문서. 저 바보 같이 생긴 수달이지만 설명 매우 굿 👍 고랭이라는 것 자체가 공식이 ..

728x90
반응형