การกำหนด struct
รวมฟิลด์ไว้ในชนิดข้อมูลเดียว
การกำหนด struct เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why You Need a struct
When values belong together, like a point's x and y, you can bundle them into one custom type. In Mojo that bundle is a struct. 📦
The struct Keyword
You define a new type with the struct keyword, a name, and a colon. Everything indented under it belongs to that type.
struct Point:
passNaming Your Type
Struct names use CapitalizedNames by convention, so Point or BankAccount. This makes your types easy to spot in code.
struct BankAccount:
passFields Hold the Data
Inside a struct you list its fields, the named pieces of data it stores. Each field is declared with var and a type.
struct Point:
var x: Int
var y: IntEvery Field Needs a Type
Mojo structs are static, so each field must state its type up front. That lets the compiler lay out memory efficiently.
var name: String
var age: IntA struct Is a Blueprint
A struct definition is just a blueprint. It describes the shape of data but does not create any actual value on its own yet.
Instances Are the Real Thing
From one blueprint you make many instances, each with its own values. The struct is the mold; instances are what comes out of it.
Grouping Beats Loose Variables
Without a struct you juggle separate x and y variables everywhere. A struct keeps related data together, so it travels as one unit.
Structs Are Value Types
In Mojo a struct is a value type. Assigning it copies the data, which keeps behavior predictable and fast by default.
A Complete Mini Struct
Here is a full struct with two fields. This single definition now describes every Point your program will ever create.
struct Point:
var x: Int
var y: IntRead It Top to Bottom
Read a struct as: here is a new type, and here is the data it carries. That mental model carries you through the whole course.
Quick Check
Let us check your grasp of defining a struct.
Recap
You met the struct: a blueprint that bundles typed fields into one value type. Next you will fill it with real data using __init__. 🎉
คำถามที่พบบ่อย
บทเรียน “การกำหนด struct” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกำหนด struct” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกำหนด struct”
รวมฟิลด์ไว้ในชนิดข้อมูลเดียว คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การกำหนด struct” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ