proto3 Data Types — Complete Reference for Field Types
In this tutorial, you will learn about proto3 data types. We cover key concepts, practical examples, and best practices to help you master this topic.
proto3 provides scalar types (int32, string, bool), Composite types (message, enum, oneof, map), and the repeated modifier for building type-safe service definitions.
Scalar Types
| Type | Description | Go | Python |
|---|---|---|---|
| double | 64-bit float | float64 | float |
| float | 32-bit float | float32 | float |
| int32 | Signed 32-bit | int32 | int |
| int64 | Signed 64-bit | int64 | int |
| uint32 | Unsigned 32-bit | uint32 | int |
| uint64 | Unsigned 64-bit | uint64 | int |
| sint32 | Signed 32-bit (efficient negative) | int32 | int |
| sint64 | Signed 64-bit (efficient negative) | int64 | int |
| fixed32 | Fixed 4 bytes | uint32 | int |
| fixed64 | Fixed 8 bytes | uint64 | int |
| sfixed32 | Signed fixed 4 bytes | int32 | int |
| sfixed64 | Signed fixed 8 bytes | int64 | int |
| bool | Boolean | bool | bool |
| string | UTF-8 string | string | str |
| bytes | Binary data | []byte | bytes |
Using Data Types
message ScanResult {
string scan_id = 1;
string device_id = 2;
int64 duration_ms = 3;
uint32 threats_found = 4;
double confidence_score = 5;
bool is_quarantined = 6;
bytes file_hash = 7;
repeated string tags = 8;
map<string, string> metadata = 9;
oneof result_type {
CleanResult clean = 10;
InfectedResult infected = 11;
ErrorResult error = 12;
}
}
Common Mistakes
1. Using int32 for Large IDs
Database IDs can exceed int32 max (2.1B). Use int64 or string (UUID) for identifiers.
2. Using String for Binary Data
Binary data in string fields wastes space (UTF-8 validation overhead). Use bytes instead.
3. Using float for Monetary Values
float has limited precision. Use double or a custom fixed-point representation for financial data.
4. Not Using sint32 for Signed Integers
sint32/sint64 use variable-length encoding optimized for negative numbers (ZigZag encoding).
5. Forgetting enum Zero Values
proto3 enum fields default to 0. Always define a UNSPECIFIED = 0 value for proper default behavior.
Practice Questions
- When should you use bytes instead of string?
- What is the difference between int32 and sint32?
- What are fixed32 and fixed64 used for?
- How does repeated work?
- When should you use uint32 vs int32?
Answers:
- Use bytes for binary data (hashes, images, encrypted data). Use string for text (names, descriptions).
- sint32 uses ZigZag encoding for efficient negative number storage. int32 treats negative numbers as large unsigned values.
- fixed32/fixed64 always use 4/8 bytes regardless of value. Use for values that are always large or need fixed-size Serialization.
repeatedcreates a list/array of the specified type. Elements are ordered and can be any number (including zero).- Use uint32 for values that cannot be negative (counts, ages, ports). Use int32 for values that may be negative.
Mini Project
Design proto3 message types for DodaTech's scan system. Include Scan, ScanResult, ScanConfig, and ScanStatus types using appropriate scalar types, enums, oneof, maps, and repeated fields.
What's Next
| Topic | Description |
|---|---|
| Services | Defining RPC services |
| Proto Syntax | Proto file structure |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro