Learn Elixir — Create Project And Run it
Create project using tools called mix,
mix new [project_name]
for example, create project “coffees”
mix new coffees
you’ll got output from command line like below
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/coffees.ex
* creating test
* creating test/test_helper.exs
* creating test/coffees_test.exsYour Mix project was created successfully.
You can use "mix" to compile it, test it, and more:cd coffees
mix testRun "mix help" for more commands.
then, go through directory
cd coffees
let’s open using your favorite’s editor :
if you’re using vs code
code .
or if you’re using atom
atom .
open file lib/coffees.ex, you’ll get the content of that file like below :
edit that file, and change the content like below
defmodule Coffees do def hello_coffee do "hi coffee lovers !" endend
let, try our first elixir program using iex (interactive elixir shell), just typing in command line
iex -S mix
then, try running “hello_coffee” function
Coffees.hello_coffee()
if you got error like this, that’s mean, you not compile elixir’s code yet.
** (UndefinedFunctionError) function Coffees.hello_world/0 is undefined or private. Did you mean one of:* hello/0(coffees) Coffees.hello_world()
to fix this, just type
recompile
you’ll got this message
iex(1)> recompile
Compiling 1 file (.ex)
:ok
then, try to rerun the elixir’s program
Coffees.hello_coffee()
if the output are like this,
iex(2)> Coffees.hello_coffee()
"hi coffee lovers !"
Congratulations, you has successfully running your first elixir’s program.