> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 기본 사항

> 네이티브 프로토콜 기본 사항

<Note>
  클라이언트 프로토콜 참고 문서는 현재 작성 중입니다.

  대부분의 예시는 Go로만 제공됩니다.
</Note>

이 문서에서는 ClickHouse TCP 클라이언트용 바이너리 프로토콜을 설명합니다.

<div id="varint">
  ## Varint
</div>

길이, 패킷 코드 및 그 밖의 경우에는 *unsigned varint* 인코딩을 사용합니다.
[binary.PutUvarint](https://pkg.go.dev/encoding/binary#PutUvarint)와 [binary.ReadUvarint](https://pkg.go.dev/encoding/binary#ReadUvarint)를 사용하십시오.

<Note>
  *Signed* varint는 사용하지 않습니다.
</Note>

<div id="string">
  ## String
</div>

가변 길이 문자열은 \*(length, value)\*로 인코딩되며, 여기서 *length*는 [varint](#varint)이고 *value*는 utf8 문자열입니다.

<Warning>
  OOM을 방지하려면 길이를 검증하십시오:

  `0 ≤ len < MAX`
</Warning>

<Tabs>
  <Tab title="인코딩">
    ```go theme={null}
    s := "Hello, world!"

    // 문자열 길이를 uvarint로 씁니다.
    buf := make([]byte, binary.MaxVarintLen64)
    n := binary.PutUvarint(buf, uint64(len(s)))
    buf = buf[:n]

    // 문자열 값을 씁니다.
    buf = append(buf, s...)
    ```
  </Tab>

  <Tab title="디코딩">
    ```go theme={null}
    r := bytes.NewReader([]byte{
        0xd, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c,
        0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21,
    })

    // 길이를 읽습니다.
    n, err := binary.ReadUvarint(r)
    if err != nil {
            panic(err)
    }

    // make()에서 OOM 또는 런타임 예외가 발생하지 않도록 n을 확인합니다.
    const maxSize = 1024 * 1024 * 10 // 10 MB
    if n > maxSize || n < 0 {
        panic("invalid n")
    }

    buf := make([]byte, n)
    if _, err := io.ReadFull(r, buf); err != nil {
            panic(err)
    }

    fmt.Println(string(buf))
    // Hello, world!
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="16진수 덤프">
    ```hexdump theme={null}
    00000000  0d 48 65 6c 6c 6f 2c 20  77 6f 72 6c 64 21        |.Hello, world!|
    ```
  </Tab>

  <Tab title="Base64">
    ```text theme={null}
    DUhlbGxvLCB3b3JsZCE
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    data := []byte{
        0xd, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c,
        0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21,
    }
    ```
  </Tab>
</Tabs>

<div id="integers">
  ## 정수
</div>

<Tip>
  ClickHouse는 고정 크기 정수형에 **Little Endian**을 사용합니다.
</Tip>

<div id="int32">
  ### Int32
</div>

```go theme={null}
v := int32(1000)

// 인코딩.
buf := make([]byte, 8)
binary.LittleEndian.PutUint32(buf, uint32(v))

// 디코딩.
d := int32(binary.LittleEndian.Uint32(buf))
fmt.Println(d) // 1000
```

<Tabs>
  <Tab title="16진수 덤프">
    ```hexdump theme={null}
    00000000  e8 03 00 00 00 00 00 00                           |........|
    ```
  </Tab>

  <Tab title="Base64">
    ```text theme={null}
    6AMAAAAAAAA
    ```
  </Tab>
</Tabs>

<div id="boolean">
  ## 불리언
</div>

불리언 값은 1바이트로 표현되며, `1`은 `true`이고 `0`은 `false`입니다.
