or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
#S:MODE=test #S:EXTERNAL=javascript=hello_holo.js=test
Hello Holo Tutorial
Let's begin with the classic Hello
WorldHolo tutorial! You will see it's super easy to create a distributed application with Holochain.Setup
hc
.macOS/Linux (you'll remember this command from the installation tutorial):
Windows (do this in the place where you installed Holochain):
Initializing your new app
Pick a new home for all your future Holochain applications to live. Something like
home_directory/holochain/
.Then create a
coreconcepts
folder for this tutorial series:Time to put the holochain command line tool (
hc
) to work and make your app.Initialize a new app and enter the app directory:
!!! note "Run in
nix-shell
"bash hc init cc_tuts cd cc_tuts
Compile
It's an always good to frequently compile your app. That way you catch any mistakes early on.
Give it a go by asking
hc
to package your app:!!! note "Run in
nix-shell
"bash hc package
Packaging your app means you are compiling the code into a DNA file and getting it ready to be run.
!!! success "You should see a successful compilation like this:"
json Created DNA package file at "/Users/username/holochain/testing_tuts/hello_holo/dist/hello_holo.dna.json" DNA hash: QmY7rhg4sf6xqQMRL1u1CnXVgmamTfxC59c9RaoFqM2eRs
Generate a zome
Your app doesn't really do too much right now because it needs a zome. A zome is Holochain's way of organizing code into nice units that perform a certain task (like saying hello).
Generate a zome called
hello
inside the zome's folder:!!! note "Run in
nix-shell
"bash hc generate zomes/hello rust-proc
Compile
!!! note "Run in
nix-shell
"bash hc package
!!! success "If all went well you should see:"
bash > cargo build --release --target=wasm32-unknown-unknown --target-dir=target Compiling hello v0.1.0 (/Users/username/holochain/core_concepts/hello_hollo/zomes/hello/code) Finished release [optimized] target(s) in 11.95s > cargo build --release --target=wasm32-unknown-unknown --target-dir=target Finished release [optimized] target(s) in 0.50s Created DNA package file at "/Users/username/holochain/core_concepts/hello_hollo/dist/hello_hollo.dna.json" DNA hash: QmdNyxke1Z9Kunws4WUXHnt4cdKQnPogC7YPpfQx67fo1z
Folder layout
Look at the folder layout
Open the
lib.rs
fileThe zome is a Rust project and makes use of macros so you can avoid writing a lot of boilerplate code. The main file you will be editing is
hello_hollo/zomes/code/src/lib.rs
.Open up the
lib.rs
file in an editor and let's have a look at the generated code.The following are all the imports. You are telling Rust, "hey, I need things from all these crates in order to do my job."
#S:INCLUDE
Next are the
use
statements. They are saying, "I want to use these specific things from the above crates." You only need a few items for this tutorial so go ahead and remove the others: #S:SKIPYou should be left with this:
#S:INCLUDE
There are a few sections of generated code that are not useful for this tutorial.
Remove the following piece of code:
#S:SKIP
The
my_zome
module is where all your zome code live.#[zome]
is a procedural macro that says that the following module defines all the things that Holochain should know about this zome. It saves you writing lots of code.Change it to
hello_zome
for this tutorial series:#S:INCLUDE,HIDE
The
init
function is run when a user starts the app for the first time. Every zome defines this function so it can do some initial setup tasks. In this zome it doesn't do anything.Return success with the empty value
()
. In Rust()
is called the unit type and is similar (though not identical) to a void type in other languages.This required function is run at application start too, once by the new user and once by the existing peers. It checks that the user is allowed to join the network. In this case it gives everyone a free pass.
#S:INCLUDE
Remove the following template code:
#S:SKIP
Add a function to say hello :)
Now tell the zome to return
Hello Holo
from a public function.Locate the
validate_agent
function:#S:SKIP
You're going to put your public zome function after it.
The
hc_public
procedural macro will turn the function directly below it into a public function that GUIs, other zomes, and DNAs can call. It takes note of the function's name, the parameters it accepts, and the type of value it returns, so Holochain can call it properly.Add the
hc_public
macro:#S:INCLUDE
The function
hello_holo
takes no arguments and returns a Holochain result type. We're also telling Holochain that if the result isOk
then it will contain a string.Start the function:
Return an
Ok
result that contains our greeting.into()
is a bit of Rust oddness that just means "turn this slice into aString
":#S:HIDE
Compile
#S:CHECK=rust
!!! note "Run in
nix-shell
"bash hc package
Talk to your app through HTTP
To interact with your application you can run it in HTTP mode.
Run your app in HTTP mode: !!! note "Run in
nix-shell
"bash hc run -i http
You can send a POST message to your app using curl, a little command for making HTTP requests. (It's included in the Holochain dev environment.)
You will need to open a new terminal window and enter the nix-shell again:
Enter the following request, which will call the
hello_holo
function and return the result:!!! note "Run in
nix-shell
"bash curl -X POST -H "Content-Type: application/json" -d '{"id": "0", "jsonrpc": "2.0", "method": "call", "params": {"instance_id": "test-instance", "zome": "hello", "function": "hello_holo", "args": {} }}' http://127.0.0.1:8888
!!! success "And you should get back your string from the
hello_holo
function:"json {"jsonrpc":"2.0","result":"{\"Ok\":\"Hello Holo\"}","id":"0"}
Congratulations –- you have created your first distributed Holochain application!
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →