LoginSignup
8
8

More than 3 years have passed since last update.

V言語コトハジメ

Last updated at Posted at 2019-06-28

The V Programming Language

公式サイトはこちら

Please note that the name of the language is "V", not "Vlang" or "V-Lang" etc.

正式名称は「V」だそうです。
「Vlang」や「V-Lang」などと呼ばないでね。ということですが、それってググラビリティ悪すぎるのでは?

ドキュメントは@MikuroXinaさんが和訳してくださっているので、こちらを参考にすると良いでしょう。
V プログラミング言語

導入

まずは導入しましょう。
いつものごとくDockerでサクッと。

kajiken@kajiken:~$ docker run --rm -it ubuntu
root@5ec2d954e645:/# apt-get update
root@5ec2d954e645:/# apt-get install vim git make curl gcc -y
root@5ec2d954e645:/#
root@5ec2d954e645:/# git clone https://github.com/vlang/v
root@5ec2d954e645:/# cd v/compiler
root@5ec2d954e645:/v/compiler# make
cc -std=gnu11 -w -o v v.c
./v -o v .
Looks like you are running V for the first time.
Setting VROOT to "/v".
root@5ec2d954e645:/v/compiler# ln -s /v/compiler/v /usr/local/bin/v
root@5ec2d954e645:/v/compiler# cd
root@5ec2d954e645:~#
root@5ec2d954e645:~# echo println\(\'Hello World\'\) > hello.v
root@5ec2d954e645:~# v hello.v 
println
println
Generating main()...
root@5ec2d954e645:~# ll hello
-rwxr-xr-x 1 root root 65824 Jun 28 0:15 hello*
root@5ec2d954e645:~# ./hello 
Hello World
root@5ec2d954e645:~# v run hello.v 
println
println
Generating main()...
============running hello==============================
Hello World
root@5ec2d954e645:~#

先にコンパイルしておいてから実行するのも、実行時にコンパイルすることも出来ます。

Web Serverを立てる

本当はこれをやりたかった。

Powerful built-in web framework [wip]

['/post/:id']
fn (b Blog) show_post(id int) vweb.Result {
  post := b.posts_repo.retrieve(id) or {
     return vweb.not_found()
  }
  return vweb.view(post)
}

コンパイラのソースを見てもvwebが見つからない。
どうやら、この部分は7月上旬まで掛かるそうです。
https://github.com/vlang/v/issues/695

出てからやりましょう。

お天気情報を取ってくる

このままHello Worldだけでは終われないので、HTTPで何か取ってくる処理を書きます。
はい、というわけでサクッと作ってみました。

weather.v
import http
import json

struct Description {
  text   string
}

struct User {
  title  string
  description Description
}

fn main() {
  data := http.get('http://weather.livedoor.com/forecast/webservice/json/v1?city=260010')
  weather := json.decode(User, data) or { return }
  println(weather.title)
  println(weather.description.text)
}
root@5ec2d954e645:~# v run weather.v 
============running weather==============================
京都府 京都 の天気
 近畿地方は、前線や湿った空気の影響でおおむね曇り、激しい雨の降っている所があります。

 今夜の京都府は、前線や湿った空気の影響で北部を中心に雨や雷雨となる所があるでしょう。北部では夜のはじめ頃まで激しく降る所がある見込み。

 明日の京都府は、前線や湿った空気の影響で曇り、北部では昼前から、南部では夜から雨となる見込みです。また、夕方まで雷を伴う所があるでしょう。
root@5ec2d954e645:~#

できた!

JSONを扱うには構造体を用意する必要があるみたいです。
複雑なJSONを使うときはちょっと面倒かもしれません。

ちなみに、VからHTTPを投げる際にはcurlを使っているようなので、エラーになったらlibcurl4-openssl-devを入れてやりましょう。

root@5ec2d954e645:~# v run weather.v 
/root//.vlang//weather.c:73:10: fatal error: curl/curl.h: No such file or directory
 #include <curl/curl.h>
          ^~~~~~~~~~~~~
compilation terminated.
V panic: clang error
root@5ec2d954e645:~# apt-get install libcurl4-openssl-dev -y
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8