-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hamed
committed
Jul 17, 2022
1 parent
d97f7c7
commit 0aba572
Showing
13 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
func main() { | ||
Print1() | ||
str := Print2() | ||
println(str) | ||
str3 := Print3("33") | ||
println(str3) | ||
} | ||
|
||
// without input and output | ||
func Print1() { | ||
println("Hello World1") | ||
} | ||
|
||
// without input and with output | ||
func Print2() string { | ||
return "Hello World2" | ||
} | ||
|
||
// with input and output | ||
func Print3(str string) string { | ||
return "Hello World " + str | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
order1, tax1 := CalculateRoomPrice("standard", 3, 2) | ||
|
||
fmt.Printf("order1 price: %d, tax1: %f\n", order1, tax1) | ||
|
||
} | ||
|
||
func CalculateRoomPrice(roomType string, nights int, personCount int) (int, float64) { | ||
var price int | ||
var tax float64 | ||
|
||
switch roomType { | ||
case "standard": | ||
price = nights * 140000 * personCount | ||
case "double": | ||
price = nights * 220000 * personCount | ||
case "suite": | ||
price = nights * 350000 * personCount | ||
} | ||
|
||
tax = float64(price) * 0.09 | ||
return price, tax | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
price, finalPrice, tax := CalculateRoomPrice("standard", 3, 2) | ||
|
||
fmt.Printf("order price: %d, tax: %f\n finalPrice = price + tax (%d + %f) => %d\n", price, tax, price, tax, finalPrice) | ||
|
||
} | ||
|
||
func CalculateRoomPrice(roomType string, nights int, personCount int) (price int, finalPrice int, tax float64) { | ||
|
||
switch roomType { | ||
case "standard": | ||
price = nights * 140000 * personCount | ||
case "double": | ||
price = nights * 220000 * personCount | ||
case "suite": | ||
price = nights * 350000 * personCount | ||
} | ||
|
||
tax = float64(price) * 0.09 | ||
finalPrice = price + int(tax) | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import "reflect" | ||
|
||
func main() { | ||
Calculator(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Ali") | ||
} | ||
|
||
func Calculator(numbers ...interface{}) { | ||
for _, item := range numbers { | ||
|
||
switch item.(type) { | ||
case int: | ||
println("int:", reflect.ValueOf(item).Int()) | ||
case string: | ||
println("string:", reflect.ValueOf(item).String()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
sum, multiply := Calculator("Ali", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | ||
|
||
fmt.Printf("sum: %d, multiply: %d\n", sum, multiply) | ||
} | ||
|
||
func Calculator(name string, numbers ...int) (sum int, mul int) { | ||
mul = 1 | ||
for _, number := range numbers { | ||
sum += number | ||
mul *= number | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
PrintLog("Ali", 1, "Reza", 3, "Error", 5, 6, "Info", 8, 9, false) | ||
|
||
} | ||
|
||
func PrintLog(logs ...interface{}) { | ||
for i, item := range logs { | ||
fmt.Printf("%d: %v\n", i, item) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
myFunc := func() { | ||
fmt.Println("Hello World") | ||
} | ||
|
||
x := 1200 | ||
|
||
myFunc() | ||
myFunc() | ||
|
||
println(func(numbers ...int) int { | ||
var total int | ||
for _, number := range numbers { | ||
total += number | ||
x++ | ||
println(&x) | ||
} | ||
return total | ||
}(1, 2, 3, 4, 5)) | ||
|
||
println(x) | ||
|
||
sum := func(numbers ...int) int { | ||
var total int | ||
for _, number := range numbers { | ||
total += number | ||
x++ | ||
println(&x) | ||
} | ||
return total | ||
} | ||
|
||
println(sum(1, 2, 3, 4, 5)) | ||
println(x) | ||
println(sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) | ||
println(x) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func main() { | ||
|
||
numbers := []int{12, 5, 48, 32, 1, 2, 74, 36, 90, 100, 95, 42} | ||
|
||
fmt.Printf("%v\n", numbers) | ||
|
||
sortingFunc := func(i, j int) bool { | ||
return numbers[i] < numbers[j] | ||
} | ||
sort.Slice(numbers, sortingFunc) | ||
|
||
fmt.Printf("%v\n", numbers) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import "time" | ||
|
||
func main() { | ||
firstName := "Ali" | ||
names := []string{"Ali", "Reza", "Peyman", "Pejman", "Pejman1", "Pejman2", "Pejman3", "Pejman4"} | ||
|
||
printFirstNameFunc := func() { | ||
println(firstName) | ||
} | ||
|
||
firstName = "Reza" | ||
|
||
printFirstNameFunc() | ||
|
||
//--------------------------------------- | ||
|
||
for i, name := range names { | ||
go func() { | ||
name = "*" + name | ||
println(i, name) | ||
}() | ||
} | ||
|
||
time.Sleep(time.Second * 1) | ||
|
||
for i, item := range names { | ||
go func(index int, name string) { | ||
name = "*" + name | ||
println(index, name) | ||
}(i, item) | ||
} | ||
|
||
time.Sleep(time.Second * 1) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
func main() { | ||
|
||
defer println("Bye") | ||
defer println("Good") | ||
|
||
for i := 0; i < 10; i++ { | ||
defer println(i) | ||
} | ||
|
||
println("Hello") | ||
|
||
CopyFile("F:\\GolangTutorial\\08-Function\\07-Defer\\destination.txt", "F:\\GolangTutorial\\08-Function\\07-Defer\\source.txt") | ||
|
||
} | ||
|
||
func CopyFile(destinationName, sourceName string) (written int64, err error) { | ||
|
||
source, err := os.Open(sourceName) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
defer source.Close() | ||
|
||
destination, err := os.Create(destinationName) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
defer destination.Close() | ||
|
||
return io.Copy(destination, source) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is test |
Oops, something went wrong.