Oneof, แผนที่ และชนิดข้อมูลที่รู้จักกันดี
จำลองข้อมูลที่ยืดหยุ่นและพัฒนาได้ด้วยฟิลด์ oneof แผนที่ และชนิดข้อมูลมาตรฐานที่รู้จักกันดี เช่น Timestamp, Duration และ Any
Oneof, แผนที่ และชนิดข้อมูลที่รู้จักกันดี เป็นบทเรียน gRPC & High Performance APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน gRPC & High Performance APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Plain Scalar Fields
Real schemas need more than strings and ints: mutually exclusive choices, key/value collections, and standard date/time types. Protobuf provides oneof, maps, and well-known types.
The oneof Construct
A oneof groups fields where at most one can be set at a time. Setting one clears the others, and only the active field is sent on the wire.
message Contact {
oneof method {
string email = 1;
string phone = 2;
}
}Reading a oneof
Generated code gives a way to check which case is set, usually a case enum or accessor, so you branch on the active field.
switch c.Method.(type) {
case *Contact_Email: useEmail(c.GetEmail())
case *Contact_Phone: usePhone(c.GetPhone())
}Map Fields
A map stores key/value pairs. Keys must be scalar (string/int); values can be any type except another map.
message Config {
map<string, string> settings = 1;
}How Maps Work on the Wire
Maps are sugar over a repeated message of key/value entries. Order is not guaranteed, and duplicate keys keep the last value — important when evolving schemas.
Well-Known Types
Protobuf ships standard types in google/protobuf/:
Timestamp— a point in timeDuration— a length of timeAny— a packed arbitrary messageStruct— dynamic JSON-like data
Timestamp and Duration
Prefer these over raw int seconds for clarity and tooling support. They map to native time types in most languages.
import 'google/protobuf/timestamp.proto';
message Event {
google.protobuf.Timestamp created_at = 1;
}Using Any
Any wraps any message plus a type URL, letting you carry heterogeneous payloads. The rich error model and reflection both use it.
import 'google/protobuf/any.proto';
message Envelope {
google.protobuf.Any payload = 1;
}FieldMask for Partial Updates
FieldMask lists which fields a request touches, enabling partial updates (PATCH-style) without ambiguity over unset vs default values.
// paths: ['name', 'email']Wrappers for Nullability
Proto3 scalars cannot distinguish unset from zero. Wrapper types like Int32Value and StringValue add explicit nullability when you need it.
Evolution Tips
When evolving these constructs:
- Add new fields to a
oneofsafely; do not reuse tag numbers - Maps: avoid changing value type
- Well-known types are stable; prefer them over hand-rolled equivalents
Quick Check
Test your advanced protobuf knowledge.
Recap
You learned advanced protobuf constructs:
oneofmodels mutually exclusive fieldsmapstores key/value pairs (sugar over repeated entries)- Well-known types:
Timestamp,Duration,Any,Struct FieldMaskenables partial updates; wrappers add nullability- Evolve carefully without reusing tag numbers
คำถามที่พบบ่อย
บทเรียน “Oneof, แผนที่ และชนิดข้อมูลที่รู้จักกันดี” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “Oneof, แผนที่ และชนิดข้อมูลที่รู้จักกันดี” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส gRPC & High Performance APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “Oneof, แผนที่ และชนิดข้อมูลที่รู้จักกันดี”
จำลองข้อมูลที่ยืดหยุ่นและพัฒนาได้ด้วยฟิลด์ oneof แผนที่ และชนิดข้อมูลมาตรฐานที่รู้จักกันดี เช่น Timestamp, Duration และ Any คุณปฏิบัติ gRPC & High Performance APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน gRPC & High Performance APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน gRPC & High Performance APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “Oneof, แผนที่ และชนิดข้อมูลที่รู้จักกันดี” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน gRPC & High Performance APIs นี้ได้ไหม
ได้ บทเรียน gRPC & High Performance APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แนวทางปฏิบัติที่ดีที่สุดสำหรับ Protobuf
- กลยุทธ์การพัฒนาสคีมา
- ตัวเลือก Protobuf แบบกำหนดเอง
- Oneof, แผนที่ และชนิดข้อมูลที่รู้จักกันดี