Go 언어 환경을 만들어 보았습니다(Gin으로 HTTP 서버 구축).

4422 단어 ginGo
요즘은 Go 언어에 대한 이야기가 많이 나와서 환경을 만들어보려고 합니다.

환경

  • Windows+VirtualBOX+Vagrant
  • 게스트 OS: CentOS7
  • 환경 구축


    Windows 측면 작업

  • 다음 Vagrantfile 작성 및 실행vagrant up
  • Vagrant.configure("2") do |config|
      config.vm.box = "centos/7"
      config.vm.network "private_network", ip: "192.168.33.20"
    end
    
  • 일어나면vagrant ssh SSH를 VM에 연결
  • 고객 운영 체제 측면 작업


    ※ Vagrantfile로 정리하면 되는데...
  • yum update
  • sudo yum install git(설치 후 Gin 필요)
  • Go 설치참고 자료
  • $ curl -OL https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
    $ sudo tar -C /usr/local -xzf go1.12.5.linux-amd64.tar.gz
    $ export PATH=$PATH:/usr/local/go/bin
    
  • Hello World 시도참고 자료
  • $ mkdir hello-world
    $ cd hello-world/
    $ sudo vi main.go
    $ go build
    $ ./hello-world
    
    main.go
    package main
    
    import "fmt"
    
    func main() {
        fmt.Printf("hello, world\n")
    }
    
    ↓↓ 결과

    HTTP 서버로 시작 시도


  • 설치Gin
  • $ go get -u github.com/gin-gonic/gin 
    
  • Gin의 QuickStart에 따라main.go 개축
  • main.go
    package main
    
    import "github.com/gin-gonic/gin"
    
    func main() {
        r := gin.Default()
        r.GET("/ping", func(c *gin.Context) {
            c.JSON(200, gin.H{
                "message": "pong",
            })
        })
        r.Run() // listen and serve on 0.0.0.0:8080
    }
    
  • 재건
  • $ go build
    $ ./hello-world
    
    ↓↓↓ 서버 시작
  • Windows 측면에서 브라우저에서 액세스http://192.168.33.20:8080/ping

  • → 잘 방문할 수 있다
    ↓ 서버 측 로그

    로그μ초 단위로 처리 속도에 자신 있는 것 같아요.(웃음)

    좋은 웹페이지 즐겨찾기