Adsız
21:14
Visual Basic - Read File Line by Line in The Visual Studio
Imports System.IO
Module Module1
Sub Main()
Dim path As String = Directory.GetCurrentDirectory()
path = Directory.GetParent(Directory.GetParent(path).ToString()).ToString()
Dim file As StreamReader = New StreamReader(path & "\\file.txt")
Dim str As String
str = file.ReadLine()
While (str IsNot Nothing)
Console.WriteLine(str.ToString())
str = file.ReadLine()
End While
Console.ReadLine()
End Sub
End Module
Adsız
02:25
Go Programming - Variables
Variables
package main import "fmt" func main() { var x string = "Hello World" fmt.Println(x) }
OR
package main import "fmt" func main() { var x string x = "Hello World" fmt.Println(x) }
$ go run main.go Hello World
package main import "fmt" func main() { var x string x = "first" fmt.Println(x) x = "second" fmt.Println(x) }
$ go run main.go
fist second
var x string = "hello" var y string = "world" fmt.Println(x == y)
This program should print
false
because hello
is not the same as world
. On the other hand:var x string = "hello" var y string = "hello" fmt.Println(x == y)This will print
true
because the two strings are the same.Since creating a new variable with a starting value is so common Go also supports a shorter statement:
x := "Hello World"
Notice the
:
before the =
and that no type was specified. The type is not necessary because the
Go compiler is able to infer the type based on the literal value you
assign the variable. (Since you are assigning a string literal, x
is given the type string
) The compiler can also do inference with the var
statement:var x = "Hello World"
The same thing works for other types:
x := 5 fmt.Println(x)
Generally you should use this shorter form whenever possible.
Example:
dogsName := "Max" fmt.Println("My dog's name is", dogsName)
$ go run main.go
My dog's name is Max
Adsız
01:57
Go Programming - Types
Integer
package main import "fmt" func main() { fmt.Println("1 + 1 =", 1 + 1) }
If you run the program and you should see this:
$ go run main.go 1 + 1 = 2
In addition to addition Go has several other operators:
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
% | remainder |
Strings
package main import "fmt" func main() { fmt.Println(len("Hello World")) fmt.Println("Hello World"[1]) fmt.Println("Hello " + "World") }
Running this program should give you:
$ go run main.go
10
e
Hello World
Booleans
&& | and |
|| | or |
! | not |
func main() { fmt.Println(true && true) fmt.Println(true && false) fmt.Println(true || true) fmt.Println(true || false) fmt.Println(!true) }Running this program should give you:
$ go run main.go true false true true false
We usually use truth tables to define how these operators work:
Expression | Value |
true && true | = true |
true && false | = false |
false && true | = false |
false && false | = false |
Expression | Value |
true || true | = true |
true || false | = true |
false || true | = true |
false || false | = false |
Expression | Value |
!true | = false |
!false | = true |
Adsız
23:03
Go Programming - Functions
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main() {
fmt.Println(add(42, 13))
}
Adsız
22:39
Go Programming - Hello World
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}