Last active
September 11, 2022 06:13
-
-
Save atulsingh0/1269d940c33e274cd012a31ff769c762 to your computer and use it in GitHub Desktop.
GoLang Snippets
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
// Go Playground | |
// https://go.dev/play/p/PxPhvxz5PQh | |
package main | |
import "fmt" | |
func isExists(list []int, item int) bool { | |
for _, v:= range list { | |
if item == v { | |
return true | |
} | |
} | |
return false | |
} | |
func main() { | |
data := []int{2, 3, 4, 1, 8, 9} | |
fmt.Println("data:", data) | |
// check if elemet 5 exists | |
out := isExists(data, 5) | |
fmt.Println("Element 5 exists in", data, out) | |
// check if elemet 8 exists | |
out = isExists(data, 8) | |
fmt.Println("Element 8 exists in", data, out) | |
} |
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
// Go Playground | |
// https://go.dev/play/p/KvVxbu_acJC | |
package main | |
import "fmt" | |
func main() { | |
emp := map[string]string{ | |
"name": "akash", | |
"age": "26", | |
"job": "contractor", | |
} | |
fmt.Printf("Emp data : %v\n", emp) | |
// Check if key "location" exists | |
val, ok := emp["location"] // ok = false | |
fmt.Printf("\nKey 'location' exists: %v\n", ok) | |
if ok { | |
fmt.Printf("Emp location is %v\n", val) | |
} else { | |
fmt.Println("Key location does not exists") | |
} | |
// Check if key "name" exists | |
val, ok = emp["name"] // ok = true | |
fmt.Printf("\nKey 'name' exists: %v\n", ok) | |
if ok { | |
fmt.Printf("Emp Name is %v\n", val) | |
} else { | |
fmt.Println("Key Name does not exists") | |
} | |
} |
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
// Go Playground | |
// https://go.dev/play/p/oxzzJOcF8JH | |
package main | |
import "fmt" | |
func main() { | |
a := 5 | |
b := 6 | |
fmt.Println(a, b) // print 5 6 | |
// Swap the var value like Python | |
a, b = b, a | |
fmt.Println(a, b) // print 6 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment