๐ก ๋ณธ ๋ฌธ์๋๋ณธ ๋ฌธ์๋ 'VSCode Golang ๊ฐ๋ฐํ๊ฒฝ ์ค์ (ChatGPT, HTTP/2)'์ ๋ํด ์ ๋ฆฌํด๋์ ๊ธ์ ๋๋ค.
Golang์ ๊ณต๋ถํ๊ธฐ์ ์์ Macbook M1์์ ๊ฐ๋ฐํ๊ฒฝ์ ์ค์ ํ๋ ๊ณผ์ ์ ์ ๋ฆฌํ์์ผ๋ฉฐ, ์ถ๊ฐ๋ก ChatGPT๋ฅผ ํ์ฉํ์ฌ HTTP/2 client, server ์ฝ๋๋ฅผ ์์ฑํ์ฌ ํ ์คํธํด๋ณด์์ผ๋ ์ฐธ๊ณ ํ์๊ธฐ ๋ฐ๋๋๋ค.
1. Go ํ๊ฒฝ ์ค์ ๋ฐ ์ค์น
ChatGPT์ ๋ฌผ์ด๋ณธ Go lang ํ๊ฒฝ ์ค์
๋จผ์ ChatGPT์๊ฒ ๋ฌผ์ด๋ณธ Go ๊ฐ๋ฐํ๊ฒฝ ์ค์ ๋ฐ ์ค์น ๋ฐฉ๋ฒ์ ๋ํด ๊ฐ๋จํ ๋ฌผ์ด๋ณด์์ต๋๋ค.
๊ทธ ๊ฒฐ๊ณผ... ์ ๋ง ์์ธํ ์๋ ค์ฃผ๋ ๊ฒ์ ํ์ธํ ์ ์์์ต๋๋ค. ๋ต๋ณ์ ๋ค์๊ณผ ๊ฐ์์ต๋๋ค.
To set up a Go project environment, you can follow these steps:
Install Go on your system. You can download and install the latest version of Go from the official Go website: https://golang.org/dl/.
Choose a directory for your project and create a new directory for your Go code. For example, you can create a directory called myproject and a subdirectory called src:
mkdir myproject
cd myproject
mkdir src
Set your Go workspace directory to the myproject directory by setting the GOPATH environment variable. For example, if you are using a Bash shell on Linux or macOS, you can add the following line to your .bashrc or .bash_profile file:
export GOPATH=/path/to/myproject
Replace /path/to/myproject with the path to your myproject directory.
Add the bin directory to your system's PATH environment variable so that you can run executables from your Go projects. For example, you can add the following line to your .bashrc or .bash_profile file:
export PATH=$PATH:$GOPATH/bin
Create a new Go module for your project using the go mod init command. For example, you can create a module called example.com/myproject:
cd src
go mod init example.com/myproject
This command creates a new go.mod file in your project directory, which specifies the dependencies for your project.
Create your Go code in the src directory. For example, you can create a file called main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
Build and run your Go code using the go build and go run commands:
go build ./myproject
# on Unix-like systems myproject.exe
# on Windows
# or
go run main.go
These commands compile and run your Go code. The go build command creates an executable file in the current directory, which you can run to execute your code. The go run command compiles and runs your code in memory without creating an executable file.
That's it! You now have a working Go project environment. You can add more Go code to your project by creating new files in the src directory, and you can use the go get command to install and manage external dependencies.
์ฌ์ค ChatGPT๊ฐ ๋ฌผ์ด๋ณด๊ธฐ ์ ์ ์ด๋ฏธ ๋ค ๊ฒฝํ์ ํด๋ณธ ์ํ์๋๋ฐ, ์ ๋ง ๊น๋ํ๊ฒ ์ ๋ฆฌ๋ฅผ ํด์ค์ ์์ผ๋ก๋ ๋ง์ด ์จ์ผ๊ฒ ๋๋ผ๊ตฌ์.
์์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์ธํ ์ ํด์ฃผ๊ณ ์๋ก ํ๋ก์ ํธ๋ฅผ ์์ฑํ ๋์๋... ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ ๋ฐ๋ณตํ์ฌ ์ธํ ํ ์ ์์ต๋๋ค.
- go mod init: module ํ์ผ ์ด๊ธฐ์์ฑ
- go get -u "": import์์ ์ฌ์ฉํ๋ module ์ ์
- *.go: go lang์ ์ด์ฉํ ํ์ผ ์์ฑ
+ VSCode Extension
2. Golang ๊ฐ๋ฐํ๊ฒฝ ํ ์คํธ
go ์ธ์ด๊ฐ ์๋ฒ ๊ฐ๋ฐ์ ์ฃผ๋ก ์ฌ์ฉํ๋ ์ธ์ด๋ค๋ณด๋๊น ์ต๊ทผ์ ๊ณต๋ถํ HTTP/2 client server ์ฝ๋๋ฅผ ๋ฌผ์ด๋ณด์์ต๋๋ค.
2.1 HTTP/2 Server Code
package main
import (
"fmt"
"log"
"net/http"
"golang.org/x/net/http2"
)
func main() {
// Create a handler to handle requests
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, HTTP/2!")
})
// Create an http2 server
server := &http.Server{
Addr: ":8080",
Handler: handler,
}
http2.ConfigureServer(server, &http2.Server{})
// Listen and serve requests
err := server.ListenAndServeTLS("server.crt", "server.key")
if err != nil {
log.Fatal("ListenAndServeTLS: ", err)
}
}โ
2.2 HTTP/2 Client Code
package main
import (
"crypto/tls"
"net/http"
"golang.org/x/net/http2"
"fmt"
"io/ioutil"
"log"
)
func main() {
// Create a new HTTP/2 client
tr := &http2.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
// Make a request to localhost:8080
req, err := http.NewRequest("GET", "https://localhost:8080", nil)
if err != nil {
panic(err)
}
// Set any additional headers or parameters here
// ...
// Send the request and get the response
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed reading response body: %s", err)
}
// Do something with the response here
// ...
fmt.Printf("Got response %d: %s %s\n", resp.StatusCode, resp.Proto, string(body))
}
2.3 HTTP/2 Server Client Code ์คํ
HTTP/2 client Server๋ฅผ ์คํํ๊ธฐ ์ํด์๋ prerequest๊ฐ ์กด์ฌํฉ๋๋ค. ๋ค์์ ๊ณผ์ ์ ํตํด ์คํํ์ฌ ํ ์คํธํ ์ ์์ต๋๋ค.
2.3.1 import ๋ชจ๋ ์ถ๊ฐ
go get -u crypto/tls net/http golang.org/x/net/http2 fmt io/ioutil log
2.3.2 cert, key ์ถ๊ฐ
HTTP/2 ํน์ฑ์ TLS ์ธ์ฆ์ ๊ฑฐ์ณ์ผ ํ๊ธฐ์ cert, key๊ฐ ํ์ํฉ๋๋ค.์ฌ๊ธฐ์ ๋ก์ปฌ ๊ฐ๋ฐ์ฉ์ผ๋ก๋ CA์ ๋์์์ด ๋๊ตฌ๋ ๊ณ ์ ์ธ์ฆ์๋ฅผ ๋ง๋ค ์ ์์ผ๋ฉฐ, localhost์ ๋ํ ๊ฐ์ธ ํค์ ์์ฒด ์๋ช ๋ ์ธ์ฆ์๋ฅผ ์์ฑํ๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ openssl ๋ช ๋ น์ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค.
openssl req -x509 -out server.crt -keyout server.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
- [Blog] Certificates for localhost: https://letsencrypt.org/ko/docs/certificates-for-localhost/
2.3.3 Server, Client ์คํ
$ go run server.go
$ go run client.go
Got response 200: HTTP/2.0 Hello, HTTP/2!โ
์ฐธ๊ณ
- [OpenAI] ChatGPT: https://chat.openai.com/chat/
- [Blog] Certificates for localhost: https://letsencrypt.org/ko/docs/certificates-for-localhost/