-
RaspberryPi로 Golang 서버만들기 - Echo Framework [4]Go 2021. 1. 3. 14:59728x90반응형
1. WorkSpace 만들기 - 작업할 폴더를 생성해줍니다.
2. main.go 파일을 생성한다. 그리고 아래코드를 입력한다.
//현재 패키지를 설정한다. main은 func main()이 있어야한다. package main //필요한 패키지를 import한다. import ( "net/http" "github.com/labstack/echo/v4" ) func main() { //echo 생성 e := echo.New() // '/'로 GET으로 요청이 왔을때 응답을 설정한다. e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }) //echo를 실행시키고, 서버가 종료됬을때 Logger를 설정한다. e.Logger.Fatal(e.Start(":1323")) }
3. 그럼 아래와 같이 나올텐데 echo package를 찾지 못해서 그렇다. go get을 통해 받아도 되지만 mod를 이용하는 것이 더 편리하다.
4. 위에 Terminal -> NewTerminal을 누른다. (RaspberryPi의 Terminal을 사용할 수 있다)
5. 아래 커맨드를 통해 init과 vendor를 통해 필요한 패키지를 다운로드 받는다.
# 작업하는 디렉토리로 이동 cd WorkSpace/ # init을 통해 현재폴더를 인식할 수 있도록 go.mod가 만들어 진다. go mod init myserver.com # vendor를 하면 소스에 import되어 있는 package를 모두 다운받는다. go mod vendor
6. 완료되면 에러가 모두 사라지면 서버를 실행 시킨다.
go run main.go
7. 테스트방법은 아래처럼 인터넷 브라우저로 해도되고, cmd중에 curl을 이용해도된다.
curl "http://192.168.0.100:1323"
728x90반응형'Go' 카테고리의 다른 글
RaspberryPi로 Golang 서버만들기 - MariaDB 설치하기 [6] (0) 2021.01.03 RaspberryPi로 Golang 서버만들기 - Postman [5] (0) 2021.01.03 RaspberryPi로 Golang 서버만들기 - visual code ssh 연결하기 [3] (0) 2021.01.03 RaspberryPi로 Golang 서버만들기 - go설치하기 [2] (0) 2021.01.03 RaspberryPi로 Golang 서버만들기 - OS 설치하기 [1] (6) 2021.01.02