Merge pull request #8 from viniciusfdasilva/dev

Create map.v file
This commit is contained in:
Vinicius Silva 2023-12-29 00:32:57 -03:00 committed by GitHub
commit faa3da2c85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 157 additions and 0 deletions

79
07_collections/map.v Normal file
View File

@ -0,0 +1,79 @@
pub fn main()
{
mut m := map[string]int{}
m['one'] = 1
m['two'] = 2
println(m['one'])
println(m['two'])
println(m['bad_key'])
println('bad_key' in m)
println(m.keys())
m.delete('two')
numbers := {
'one' : 1
'two' : 2
}
println(numbers)
// ====================================================== //
sm := {
'abc' : 'xyz'
}
val := sm['bad_key']
println(val)
intm := {
1: 1234
2: 5678
}
s := intm[3]
println(s)
// ====================================================== //
mm := map[string]int{}
val3 := mm['bad_key'] or { panic('key not found') }
println(val3)
m1 := {
'abc' : 'def'
}
if v := m1['abc']{
println('the map value for that key is ${v}')
}
// ====================================================== //
arr := [1,2,3]
large_index := 999
val6 := arr[large_index] or { panic('out of bounds') }
println(val6)
val2 := arr[333]!
println(val2)
// ====================================================== //
mut m2 := map[string]map[string]int{}
m2['greet'] = {
'Hello' : 1
}
m2['place'] = {
'World' : 2
}
m2['code']['orange'] = 123
println(m2)
}

View File

@ -0,0 +1,5 @@
import crypto.sha256 as v_sha256
pub fn main(){
println('oi')
}

View File

@ -0,0 +1,6 @@
import os
pub fn main(){
name := os.input('Type your name')
println(name)
}

View File

@ -0,0 +1,9 @@
import os { input, user_os }
pub fn main(){
name := input('Type yout name')
println('Nane ${name}')
current_os := user_os()
println('Your OS is ${current_os}')
}

1
09_defer/log.txt Normal file
View File

@ -0,0 +1 @@
This is a log file

16
09_defer/main.v Normal file
View File

@ -0,0 +1,16 @@
import os
pub fn main(){
ok := false
mut f := os.open('log.txt') or { panic('File cannot be read!') }
defer{
f.close()
}
if !ok{
return
}
}

41
09_defer/state.v Normal file
View File

@ -0,0 +1,41 @@
import os { create }
enum State{
normal
write_log
return_error
}
fn write_log(state State) !int {
mut f := create('log.txt')!
defer {
f.close()
println('File closed')
}
match state{
.normal{
return f.writeln("This is a normal file")
}
.write_log {
return f.writeln("This is a log file")
}
.return_error{
return error('nothing written; file open: ${f.is_opened}')
}
}
return 0
}
pub fn main(){
n := write_log(.write_log) or {
println('Error ${err}')
0
}
println('${n} bytes written')
}