![[Learn Go with Tests] Hello World 및 테스트 실행 방법](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2F6apzV%2FbtsIDojLPan%2FAAAAAAAAAAAAAAAAAAAAACAod4PZfv33tauLGEjWgQ62SEmMSustOxTcAXAQ7xfo%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1756652399%26allow_ip%3D%26allow_referer%3D%26signature%3D44YITyCPAf08zu%252FuYn%252B0PoVmNuo%253D)
[Learn Go with Tests] Hello World 및 테스트 실행 방법Language/Go2024. 7. 18. 00:41
Table of Contents
728x90
해당 포스팅은 Learn Go with Tests Gitbook을 따라 실습한 내용을 정리한 문서입니다.
Hello World!
hello 디렉토리에 hello.go 파일을 만들어 준다.
package main
import "fmt"
func main() {
fmt.Println("Hello, world")
}
go run hello.go 하면 실행이 가능하다.
이렇게 실행이 되는 모습을 볼 수 있다.
Refactor
package main
import "fmt"
func Hello() string {
return "Hello, world"
}
func main() {
fmt.Println(Hello())
}
- 새로운 함수 만들기 → func() 를 사용한다.
- string → 함수의 반환형이 string임을 의미한다.
How to Test
package main
import "testing"
func TestHello(t *testing.T) {
got := Hello()
want := "Hello, world"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
다음과 같이 프린트문을 비교하여 Test 코드를 작성할 수 있다.
go의 test의 경우 테스트 하려고 하는 go 파일에 _test를 넣어 hello_test.go로 네이밍하여 파일을 만든다.
How to Work?
$ go test
go: cannot find main module; see 'go help modules'
go test를 하려고 하면 module과 관련된 에러가 나온다. 이를 해결하려면 다음과 같이 따라서 해야 한다.
go mod init hello
⇒ 다음과 같이 go.mod 파일이 생긴 것을 볼 수 있다. 이제 go test 혹은 go build가 가능할 것이다.
728x90
'Language > Go' 카테고리의 다른 글
[Learn Go with Tests] 포인터 & 에러 (1) | 2024.07.24 |
---|---|
[Learn Go with Tests] 구조체, 메서드 & 인터페이스 (0) | 2024.07.18 |
[Learn Go with Tests] 슬라이스 및 배열 (0) | 2024.07.18 |
[Learn Go with Tests] Iteration (0) | 2024.07.18 |
[Learn Go with Tests] Integer (1) | 2024.07.18 |
@잉퓨_ :: 대학로에서 개발하기
보안 전공 개발자지만 대학로에서 살고 싶어요
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!