Merge pull request #9 from viniciusfdasilva/dev
10_goto directory created
This commit is contained in:
commit
01526f14af
|
|
@ -0,0 +1,15 @@
|
|||
pub fn main(){
|
||||
|
||||
x := true
|
||||
y := false
|
||||
|
||||
if x {
|
||||
if !y {
|
||||
unsafe { goto my_label }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
my_label:
|
||||
println('oi')
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
struct Foo{
|
||||
a int
|
||||
mut:
|
||||
b int
|
||||
c int
|
||||
pub:
|
||||
d int
|
||||
pub mut:
|
||||
e int
|
||||
__global:
|
||||
f int
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
println("OI")
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
struct Book{
|
||||
author struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
title string
|
||||
}
|
||||
|
||||
|
||||
pub fn main(){
|
||||
book := Book{author: struct{name: 'Samantha Black', age: 23}, title: 'Livro'}
|
||||
|
||||
assert book.author.name == 'Samantha Black'
|
||||
assert book.author.age == 23
|
||||
assert book.title == 'Livro'
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
struct Foo{
|
||||
n int // Default is 0
|
||||
s string // Default is ''
|
||||
a []int // Default is []int{}
|
||||
pos int = -1 // Default is -1
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
mut f := Foo{1,'Test', [12,3,5,3,6], -2}
|
||||
println(f)
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
struct Size{
|
||||
mut:
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
fn (s &Size) area() int{
|
||||
return s.width * s.height
|
||||
}
|
||||
|
||||
struct Button{
|
||||
Size
|
||||
title string
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
mut button := Button{title: 'Click me', height: 2}
|
||||
button.width = 3
|
||||
|
||||
assert button.area() == 6
|
||||
assert button.Size.area() == 6
|
||||
println(button)
|
||||
|
||||
mut button1 := Button{Size: Size{
|
||||
width : 34
|
||||
height: 10
|
||||
}}
|
||||
|
||||
println(button1)
|
||||
|
||||
button1.Size = Size{width: 2, height: 9}
|
||||
println(button1)
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
struct Point{
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn main(){
|
||||
p := &Point{10,10}
|
||||
println(p.x)
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
struct Foo{
|
||||
mut:
|
||||
x int
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
fa := Foo{1}
|
||||
mut a := fa
|
||||
a.x = 2
|
||||
assert fa.x == 1
|
||||
assert a.x == 2
|
||||
|
||||
mut fc := Foo{1}
|
||||
mut c := &fc
|
||||
|
||||
c.x = 2
|
||||
assert fc.x == 2
|
||||
assert c.x == 2
|
||||
|
||||
println(fc)
|
||||
println(c)
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
|
||||
struct Point{
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
mut p := Point{x: 10, y: 20}
|
||||
|
||||
assert p.x == 10
|
||||
|
||||
mut p1 := Point{x: 30, y: 4}
|
||||
|
||||
assert p1.y == 4
|
||||
point := [Point{x: 10, y: 20},Point{x: 20, y: 30},Point{x: 30, y: 40}]
|
||||
println(point)
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
struct User{
|
||||
age int
|
||||
}
|
||||
|
||||
fn (u User) can_register() bool{
|
||||
return u.age > 16
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
|
||||
user1 := User{age: 10}
|
||||
|
||||
println(user1.can_register())
|
||||
|
||||
user2 := User{age: 20}
|
||||
|
||||
println(user2.can_register())
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
struct Foo{
|
||||
m int @[required]
|
||||
n string
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
mut a := Foo{1, ''}
|
||||
assert a.m == 1
|
||||
|
||||
_ := Foo{1,'Test'}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import sample
|
||||
|
||||
fn main(){
|
||||
//mut into1 := sample.Information{data: 'Sample'}
|
||||
//println(into1)
|
||||
info := sample.new_information('Sample information')!
|
||||
println(info)
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
module sample
|
||||
|
||||
@[noinit]
|
||||
pub struct Information{
|
||||
pub:
|
||||
data string
|
||||
}
|
||||
|
||||
pub fn new_information(data string) !Information{
|
||||
|
||||
if data.len == 0 || data.len > 100{
|
||||
return error('data must be between 1 and 100 characters')
|
||||
}else{
|
||||
return Information{
|
||||
data: data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
struct User{}
|
||||
|
||||
fn User.new() User{
|
||||
return User{}
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
user := User.new()
|
||||
println(user)
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
struct Point{
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
mut p := Point{x: 10, y: 20}
|
||||
println('${p.x} | ${p.y}')
|
||||
|
||||
|
||||
p1 := Point{10, 20}
|
||||
assert p1.x == 10
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
@[params]
|
||||
struct ButtonConfig{
|
||||
text string = 'oi'
|
||||
is_disabled bool
|
||||
width int = 70
|
||||
height int //= 20
|
||||
}
|
||||
|
||||
struct Button{
|
||||
text string
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
fn new_button(c ButtonConfig) &Button{
|
||||
return &Button{
|
||||
width: c.width
|
||||
height: c.height
|
||||
text: c.text
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
button := new_button()
|
||||
assert button.height == 0
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
struct User{
|
||||
name string
|
||||
age int
|
||||
is_registered bool
|
||||
}
|
||||
|
||||
fn register(u User) User{
|
||||
return User{
|
||||
...u
|
||||
is_registered: true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main(){
|
||||
mut user := User{
|
||||
name: 'abc'
|
||||
age: 23
|
||||
}
|
||||
|
||||
user = register(user)
|
||||
println(user)
|
||||
}
|
||||
Loading…
Reference in New Issue