> ## 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.

# Basics

> Native protocol basics

<Note>
  Client protocol reference is in progress.

  Most examples are only in Go.
</Note>

This document describes binary protocol for ClickHouse TCP clients.

<h2 id="varint">
  Varint
</h2>

For lengths, packet codes and other cases the *unsigned varint* encoding is used.
Use [binary.PutUvarint](https://pkg.go.dev/encoding/binary#PutUvarint) and [binary.ReadUvarint](https://pkg.go.dev/encoding/binary#ReadUvarint).

<Note>
  *Signed* varint isn't used.
</Note>

<h2 id="string">
  String
</h2>

Variable length strings are encoded as *(length, value)*, where *length* is [varint](#varint) and *value* is utf8 string.

<Warning>
  Validate length to prevent OOM:

  `0 ≤ len < MAX`
</Warning>

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

    // Writing string length as uvarint.
    buf := make([]byte, binary.MaxVarintLen64)
    n := binary.PutUvarint(buf, uint64(len(s)))
    buf = buf[:n]

    // Writing string value.
    buf = append(buf, s...)
    ```
  </Tab>

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

    // Read length.
    n, err := binary.ReadUvarint(r)
    if err != nil {
            panic(err)
    }

    // Check n to prevent OOM or runtime exception in make().
    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="Hex dump">
    ```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>

<h2 id="integers">
  Integers
</h2>

<Tip>
  ClickHouse uses **Little Endian** for fixed size integers.
</Tip>

<h3 id="int32">
  Int32
</h3>

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

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

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

<Tabs>
  <Tab title="Hex dump">
    ```hexdump theme={null}
    00000000  e8 03 00 00 00 00 00 00                           |........|
    ```
  </Tab>

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

<h2 id="boolean">
  Boolean
</h2>

Booleans are represented by single byte, `1` is `true` and `0` is `false`.
