Skip to content

Instantly share code, notes, and snippets.

@bitsnaps

bitsnaps/hello.v Secret

Last active September 17, 2019 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsnaps/98b59cf8f6ad0b698110fd15ddf91395 to your computer and use it in GitHub Desktop.
Save bitsnaps/98b59cf8f6ad0b698110fd15ddf91395 to your computer and use it in GitHub Desktop.
V language summary
// updated for v0.1.19
//import http // need to install openssl
import json
const (
PI = 3.14
NUMBERS = [1, 2, 3]
)
struct Point {
x int
y int
}
fn (p Point) draw() string {
return 'Point${p.str()}' // uses str()
}
fn (p Point) str() string {
return '(${p.x},${p.y})'
}
interface Drawabler { draw() string }
enum Color { Blue, Green, Red }
fn main() {
mut lang := 'V!'
println('Hello $lang')
lang = 'V will warn you if you dont change mutable variable'
// Type Cast
is_true := bool(true) // now variable names cannot contain uppercase letters only snake_case
println(is_true)
os := 'linux'
println(os)
my_int := int(23)
println(my_int)
println(NUMBERS)
for n in NUMBERS {
println(n)
}
array1 := [2, 4, 5]
for i := 0; i < 3; i++ {
println(array1[i])
}
switch os {
case 'darwin':
println('macOS.')
case 'linux':
println('Linux.')
default:
println(os)
}
p := Point{x: 2, y: 3}
println(p.draw())
// show p with str()
println(p)
p2 := &Point{x: 7, y: 9}
println(p2.x)// Pointers have the same syntax for accessing fields
mut color := Color.Red
println(color)
color = Color.Green
// JSON Decode
data := '{ "x": 2, "y": 3 }'
println(data)
point := json.decode(Point, data) or {
eprintln('Failed to decode json')
return
}
println(point)
// Assertion
assert point.draw() == 'Point(2,3)'
// Matrices
m1 := [[2, 4, 5],[5,3,6]]
// Assertion
assert m1[0][0] == 2
// Working with http
/*html := http.get_text('https://news.ycombinator.com')
mut pos := 0
for {
pos = html.index_after('https://', pos + 1)
if pos == -1 {
break
}
end := html.index_after('"', pos)
println(html.substr(pos, end))
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment