Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

Contents

Go 1.16 발표

2021년 2월 16일 Go 1.16이 발표됐다. Go 1.16은 []https://golang.org/dl/ 다운로드 페이지]에서 다운로드 할 수 있다. 곧바로 설치했다.
# wget  https://golang.org/dl/go1.16.linux-amd64.tar.gz
# tar -xvzf go1.16.linux-amd64.tar.gz /usr/local 
# go version
go version go1.16 linux/amd64

1.16의 주요 기능은 아래와 같다.
  • embed package : 컴파일 할적에 데이터 파일을 go 프로그램에 번들 할 수 있다고 한다. 괜찮은 기능 같다. 바로 테스트 하기로 했다.
  • macOS ARM64 지원 : 그렇다고 한다. 리눅스 사용자라서 관심이 없다.
  • Go module을 기본으로 사용한다고 한다. 전체 go 개발자의 96% 정도가 go module을 사용한다고 한다. 패키지 관리 관련 고민을 할 필요가 없어진 것 같다.
  • 빌딩속도가 25% 빨라지고, 메모리도 15%정도 덜 사용한다. 라고 한다.

embed package

코드에서 //go:embed 문을 사용해서 컴파일 타임에 패키지 디렉토리 혹은 다른 하위 디렉토리에서 읽은 파일로 문자열, []byte 또는 FS 유형의 변수를 초기화 할 수 있다. 사용 방법은 이렇다.
import _ "embed"

//go:embed hello.txt
var s string
print(s)

여러 개의 파일과 디렉토리를 포함 할 수 있으며, 와일드카드 문자도 사용 할 수 있다.
//go:embed hello.txt tmpl/*
var f embed.F

간단한 테스트 코드
package main

import (
	"embed"
	"fmt"
)

// main.go 파일의 내용을 읽어서 string 변수에 저장한다.
//go:embed main.go
var code string

// hello.txt, tmpl/* 를 FS 변수에 저장한다.
//go:embed hello.txt tmpl/*
var f embed.FS

func main() {
	fmt.Println(code)
	data, _ := f.ReadFile("hello.txt")
	fmt.Println(string(data))
	data, _ = f.ReadFile("tmpl/hello.tmpl")
	fmt.Println(string(data))
}

이렇게 만든 코드를 빌드하면 파일이 실행파일에 embed 되기 때문에, "실행파일 하나만 배포" 하면 된다. 버전관리도 편해졌다. 예전에는 ldflag를 이용해서 관리했는데, embed 패키지로 깔끔하게 관리 할 수 있게 됐다.
# cat version.txt
1.0.0-as99dhj
package main

import (
	_ "embed"
	"fmt"
)

//go:embed version.txt
var version string

func main() {
	fmt.Print(version)
}

embed 예제

HTML 문서를 포함하는 간단한 웹 애플리케이션을 만들기로 했다. 이 웹 애플리케이션은 정적문서(HTML, CSS)를 서빙한다. 디렉토리 구성은 아래와 같다.
.
├── card
│   ├── card.html
│   └── default.css
├── go.mod
├── index.html
└── main.go

main.go 코드다.
package main

import (
	"embed"
	"net/http"
)

//go:embed *.html card/*
var webUI embed.FS

func main() {
	http.Handle("/", http.FileServer(http.FS(webUI)))
	http.ListenAndServe(":8888", nil)
}

card.html
<!DOCTYPE html>
<HTML lang="en">

<head>
    <!-- Compressed CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites@6.6.3/dist/css/foundation.min.css"
        integrity="sha256-ogmFxjqiTMnZhxCqVmcqTvjfe1Y/ec4WaRj/aQPvn+I=" crossorigin="anonymous">
    <link rel="stylesheet" href="/card/default.css">
    <title>Card</title>
</head>

<body>
    <div class="grid-x">
        <div class="cell large-12">
            <ul class="pricing-table">
                <li class="title">Enterprise</li>
                <li class="price">$99.99</li>
                <li class="description">An awesome description</li>
                <li>42 Rad Features</li>
                <li>7GB of Power</li>
                <li><a class="button" href="#">Buy Now</a></li>
            </ul>
        </div>
    </div>
    <script>
        $(document).foundation();
    </script>
</body>
</HTML>

default.css

.pricing-table {
    background-color: #fefefe;
    border: solid 1px #cacaca;
    text-align: center;
    list-style-type: none;
  }
  
  .pricing-table li {
    border-bottom: dotted 1px #cacaca;
    padding: 0.875rem 1.125rem;
  }
  
  .pricing-table li:last-child {
    border-bottom: 0;
  }
  
  .pricing-table .title {
    background-color: #0a0a0a;
    color: #fefefe;
    border-bottom: 0;
  }
  
  .pricing-table .price {
    background-color: #e6e6e6;
    font-size: 2rem;
    border-bottom: 0;
  }
  
  .pricing-table .description {
    color: #8a8a8a;
    font-size: 80%;
  }
  
  .pricing-table :last-child {
    margin-bottom: 0;
  }

index.html은 그냥 Hello world를 출력했다.
<h1> Hello World </h1>

빌드하고 웹 브라우저로 접근해보자.

 embed 예제