官术网_书友最值得收藏!

Creating a simple application

In this recipe, we will be using Mix to create a new application.

How to do it…

To create a new Elixir application, follow these steps:

  1. In a command-line session, enter mix help to see a list of available tasks:
    > mix help
    

    Here is what the screen will look like:

    How to do it…
  2. To generate a new application, type mix new simple_app:
    > mix new simple_app
    

    What happens next is shown in the following screenshot:

    How to do it…
  3. Inside the simple_app directory, the generated application is ready to be started. Run iex –S mix to start the application and verify that everything is working:
    > iex -S mix
    Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
    Interactive Elixir (0.15.1) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> 
    
  4. Nothing happened. So is it working? The absence of messages in the IEx session is a good thing. This generated application behaves more like a library; there's no main function like in Java or C. To be sure that the application is responding, edit the lib/simple_app.ex file by inserting the following code:
    defmodule SimpleApp do
      def greet do
        IO.puts "Hello from Simple App!"
      end
    end
  5. Restart the application by pressing Ctrl + C twice and entering iex –S mix again.
  6. In the IEx session, enter SimpleApp.greet.
  7. You will see the following output from the application:
    iex(1)> SimpleApp.greet
    Hello from Simple App!
    :ok
    iex(2)>
    

The Elixir application is ready to be used either on your local machine or, if a node is started, it could even be used from another machine.

How it works…

The Elixir installation provides a command-line tool called Mix. Mix is a build tool. With this tool, it is possible to invoke several tasks to create applications, manage their dependencies, run them, and more.

Mix even allows the creation of custom tasks.

See also

  • To generate an OTP application with a supervisor, see the Generating a supervised application recipe.
主站蜘蛛池模板: 宁化县| 岳普湖县| 汉中市| 常德市| 霍林郭勒市| 夏河县| 宜城市| 水富县| 巫山县| 会同县| 黄平县| 靖远县| 凌源市| 黔西县| 贵定县| 金塔县| 抚宁县| 广河县| 平乐县| 石嘴山市| 富蕴县| 始兴县| 安庆市| 南皮县| 西宁市| 上思县| 磴口县| 平凉市| 浦县| 宁化县| 揭阳市| 丹阳市| 平乡县| 长寿区| 门源| 堆龙德庆县| 会昌县| 高平市| 贵港市| 昌平区| 常熟市|