Mengenal Motoko, Bahasa Pemrograman di Internet Computer (ICP)

Pelajari langkah awal menggunakan Motoko, bahasa pemrograman untuk Internet Computer. Panduan sederhana untuk anak usia 12 tahun.

Halo, teman-teman! Pernah dengar tentang Internet Computer atau bahasa pemrograman bernama Motoko? Nah, di artikel ini kita akan belajar langkah pertama membuat proyek sederhana menggunakan Motoko. Yuk, kita mulai petualangan seru ini!

Apa Itu Internet Computer dan Motoko?

Internet Computer adalah jaringan blockchain yang memungkinkan kita membuat aplikasi terdesentralisasi dengan cara yang lebih efisien. Nah, Motoko adalah bahasa pemrograman khusus yang dirancang untuk menulis kode di Internet Computer.

Motoko dibuat agar mudah digunakan oleh pemula, tetapi juga cukup kuat untuk membangun aplikasi yang kompleks. Kita akan mempelajari dasar-dasarnya dengan membuat proyek kecil.

Membuat Proyek Baru di Internet Computer

Langkah pertama untuk memulai adalah membuat proyek baru. Ikuti langkah-langkah berikut:

  1. Persiapkan Folder Proyek

    • Buka terminal di komputer kamu.
    • Masuk ke folder tempat kamu ingin menyimpan proyek. Contohnya: cd ic-projects.
  2. Buat Proyek Baru

    • Ketik perintah berikut:
      dfx new dbank
      
    • Ini akan membuat proyek baru bernama dbank. Tunggu beberapa menit hingga semua file dan folder selesai dibuat.
  3. Masuk ke Folder Proyek

    • Setelah proyek selesai dibuat, masuk ke folder proyek:
      cd dbank
      

Membuka Proyek di VS Code

Setelah proyek dibuat, kita perlu membukanya di teks editor seperti VS Code:

  1. Buka VS Code di komputer kamu.
  2. Klik Open Folder dan pilih folder proyek dbank.
  3. Di dalam folder proyek, cari folder bernama src. Di sini semua kode akan ditulis.

Memulai dengan File Motoko

Di dalam folder src, kamu akan menemukan file bernama main.mo. File ini adalah tempat kita menulis kode. Untuk memulai dari nol, hapus semua kode di file ini. Sekarang, mari kita tulis kode pertama kita!

  1. Membuat Aktor

    • Di Motoko, sebuah aplikasi disebut aktor. Kita bisa membuatnya seperti ini:
      actor DBank {
      };
      
    • Ini adalah kerangka dasar dari aplikasi kita.
  2. Menambahkan Variabel

    • Tambahkan variabel untuk menyimpan data, misalnya jumlah uang di bank:
      var currentValue: Nat = 300;
      
    • Nat adalah tipe data untuk angka positif. Kita menyimpan angka 300 sebagai saldo awal.

Debugging dan Mencetak Nilai

Untuk memastikan kode kita berjalan, kita bisa menggunakan Debug module untuk mencetak nilai di terminal:

  1. Impor Debug Module

    • Tambahkan baris ini di awal kode:
      import Debug "mo:base/Debug";
      
  2. Mencetak Pesan

    • Gunakan fungsi Debug.print untuk mencetak pesan:
      Debug.print("Halo, ini adalah DBank!");
      
  3. Mencetak Variabel

    • Jika ingin mencetak variabel, gunakan fungsi debug_show:
      Debug.print(debug_show(currentValue));
      

Menjalankan Proyek

Setelah selesai menulis kode, saatnya menjalankan proyek:

  1. Mulai Internet Computer Lokal

    • Di terminal, ketik:
      dfx start
      
    • Ini akan memulai simulasi Internet Computer di komputer kamu.
  2. Deploy Proyek

    • Buka terminal baru (jangan tutup terminal sebelumnya) dan ketik:
      dfx deploy
      
    • Ini akan mengunggah dan menjalankan proyek di simulasi Internet Computer.
  3. Cek Output

    • Jika kode kamu benar, pesan dan nilai akan muncul di terminal.

Variabel dan Konstanta di Motoko

Di Motoko, ada dua cara menyimpan data: variabel dan konstanta.

  1. Variabel

    • Digunakan untuk data yang bisa berubah.
      var currentValue: Nat = 300;
      currentValue := 100; // Ubah nilai menjadi 100
      
  2. Konstanta

    • Digunakan untuk data yang tidak berubah.
      let id: Nat = 12345;
      // Tidak bisa mengubah nilai id
      

Tantangan Kecil: Cetak ID Konstanta

Sekarang giliran kamu! Cobalah mencetak nilai dari konstanta id menggunakan Debug.print. Langkah-langkahnya:

  1. Buat konstanta dengan nilai angka.
  2. Gunakan debug_show untuk mencetak nilai ke terminal.

Kesimpulan

Selamat! Kamu baru saja membuat proyek pertama di Internet Computer menggunakan Motoko. Kita sudah belajar cara:

  • Membuat proyek baru.
  • Menulis kode dasar.
  • Menggunakan variabel dan konstanta.
  • Debugging dengan mencetak nilai.

Di artikel berikutnya, kita akan membuat aplikasi yang lebih menarik, seperti menambahkan fitur deposit dan penarikan di dbank. Sampai jumpa, dan selamat mencoba! 😊

Comments

Postingan Populer

Image

Superingan is a Blogger template designed with simplicity and functionality in mind, offering a clean and user-friendly platform for professional websites. This theme is perfect for various website types, including personal blogs, online magazines, news websites, and portfolios. Features: Responsive Design: Your website will look its best across all devices, from desktops and laptops to tablets and smartphones. Customizable Colors: Easily change the template's colors to match your branding or preferences. Custom Headers: Upload your own header image to add a personal touch to your website. Custom Menus: Create and manage your website's navigation menus with ease. Widget Areas: Add widgets to display additional content and features on your website's sidebar. Clean HTML & CSS Code: The template is built with clean and well-structured code, making it easy to learn and customize further. Superingan is an ideal choice for those seeking a professional website with a sim...

Image

If you’ve ever struggled to align elements neatly in CSS—or tried to make a layout look good on both desktop and mobile— Flexbox CSS is your new best friend. Short for "Flexible Box Layout," Flexbox is a layout module in CSS3 that provides an efficient way to distribute space and align items in a container, even when their sizes are dynamic. In this flexbox tutorial for beginners , we’ll guide you step-by-step through what Flexbox is, why it’s essential for responsive design, and how to use it to build clean, adaptable layouts. By the end of this guide, you’ll be able to use Flexbox confidently to build elements like navigation bars, feature sections, and even a CSS flexbox pricing table example . Whether you're a self-taught developer, a student, or someone shifting into a front-end career, Flexbox will become one of your most powerful tools in building responsive, user-friendly websites. Before diving into the code, if you're not yet familiar with CSS fundamenta...

Image

Hello everyone! In our last post about Grid Layouts , we explored how to create a basic grid structure in CSS. Now, let’s take that one step further. Today, we're diving into Grid Sizing — how to size columns and rows inside your grid layout. By the end of this post, you’ll know exactly when to use fixed units like px , flexible units like fr , and responsive functions like minmax() . We’ll even explore some cool developer tools and an interactive test. Ready? Let’s go! What is Grid Sizing? Grid Sizing refers to how we control the size of the rows and columns inside our CSS Grid layout. Depending on what kind of content you're building (e.g., dashboards, cards, galleries), you'll want your grid to behave differently. In CSS Grid, we use properties like: grid-template-rows grid-template-columns grid-auto-rows grid-auto-columns Let’s break these down together. 1. Fixed Sizes with px and rem You can define static sizes for rows and columns using pixels ...