0Pricing
Mojo Academy · Lección

Definir una struct

Agrupe campos en un solo tipo.

Definir una struct es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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:
    pass

Naming Your Type

Struct names use CapitalizedNames by convention, so Point or BankAccount. This makes your types easy to spot in code.

struct BankAccount:
    pass

Fields 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: Int

Every 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: Int

A 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: Int

Read 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__. 🎉

Preguntas frecuentes

¿La lección «Definir una struct» es gratis?

Sí — el texto completo de «Definir una struct» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Definir una struct»?

Agrupe campos en un solo tipo. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Definir una struct»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Definir una struct
  2. Campos y el método __init__
  3. Añadir métodos a una struct
  4. Structs frente a clases de Python
← Volver a Mojo Academy