Skip to main content
  1. Blogs/

Mojo

2 mins· 0 · 0 ·
new tech programming languages mojo
Table of Contents
New Programming Language (Mojo) trial

Why Try Mojo 為何想嘗試一下Mojo #

  1. Because saw this article Mojo Language: The New Programming Language for AI

一直在尋求除了Python以外其他的AI開發用程式語言,Python 的performance 極差造成在邊緣運算的開發瓶頸,因此在嘗試的使用其他跟硬體整合度更好的高階程式語言 (不需要做Cross compiler 額外的編譯) e.g. GO, RUST.

Feature Python Go
Programming language General-purpose General-purpose
Type system Dynamic Static
Concurrency Yes Yes
Garbage collection Yes Yes
Memory safety Yes Yes
Performance Interpreted, slower Compiled, faster
Simplicity Easier to learn and use More complex to learn, but simpler syntax
Community Large and active Smaller but growing
Popular use cases Web development, data science, machine learning Systems programming, cloud computing, distributed systems
  1. Just like to learn new programming languages 💖💕🫶

Mojo Installation (VS Code + WSL) #

installation reference

  1. Install VS Code, the WSL extension. (also see Python Dummy Day 1)
  2. Install Ubuntu 22.04 for WSL and open it.
  3. In the Ubuntu terminal, install the Modular CLI:
curl https://get.modular.com | \
  MODULAR_AUTH=mut_0056b5ebbaab4074af12927c8d9af244 \
  sh -
  1. Install the Mojo SDK:
modular install mojo
  1. Open VS Code, connect to WSL, and install VS Code Mojo Extension.

    Mojo VS Code Extension

  2. Check Mojo in shell initialization ( ~/.zshrc as an example)

export PATH=$PATH:/usr/local/go/bin
export MODULAR_HOME="$HOME/.modular"
export PATH="$MODULAR_HOME/pkg/packages.modular.com_mojo/bin:$PATH"

if not in rc script, run commands:

echo 'export MODULAR_HOME="$HOME/.modular"' >> ~/.zshrc

echo 'export PATH="$MODULAR_HOME/pkg/packages.modular.com_mojo/bin:$PATH"' >> ~/.zshrc

source ~/.zshrc

First Mojo Program : Hello World! #

hello world reference

Run code in the REPL(Read-eval-print loop) #

  1. Run mojo execution environment
~/work/mojo » mojo                                                                                                                                                              
Welcome to Mojo! 🔥
Expressions are delimited by a blank line.
Type `:mojo help` for further assistance.
1> 
  1. print hello world
1> print("hello world")      ### not allow backspace
2.                           ### press enter here                           
hello world
2> 

Build and run Mojo source files #

  1. create a hello work folder
~/work/mojo » mkdir hello 
  1. create a hello.mojo file
~/work/mojo/hello » code hello.mojo
  1. write hello world mojo codes in hello.mojo file
fn main():
   print("Hello, world!")
  1. run hello.mojo
~/work/mojo/hello » mojo hello.mojo
Hello, world!
  1. build hello world executable binary
~/work/mojo/hello » mojo build hello.mojo
  1. check generated executable binary file
~/work/mojo/hello » ls
hello  hello.mojo
  1. run hello
~/work/mojo/hello » ./hello
Hello, world!

不負責之試用心得 #

  1. binary size : 1.7 MB,比之前用GO build做出來還小一些,但是差距不大。
~/work/mojo/hello » ls -alh hello
-rwxr-xr-x 1 anitawu anitawu 1.7M Sep 23 13:51 hello
  1. 稍微看一下它包了什麼進去
~/work/mojo/hello » hexdump -C hello | more

Link GNU

link GNU
一堆linux system call
linux system call
link glibc 不同版本,為跨平台。
glibc
一堆llvm
llvm

期待能整合到各家GPU/APU/NPU whatever Unit。應該是跟RUST 不同走向。語法其實跟python 比較類似,學起來不會太久。下次來試試其他的example (看看有什麼演算法或是training相關的)。


Reference

Related

Python Dummy Day 1 : 開發環境
2 mins· 0 · 0
training python dummy
如何使用 WSL 在 Windows 上安裝 Linux 如何使用 WSL 在 Windows 上安裝 Linux # 安裝 WSL # Windows 搜尋 Powershell # 開啟powershell 安裝WSL # wsl --install Hacking!