Well, I created a slice of int like this:
list_of_id := []string {1,2,3,4}
My code would do a check if a variable in my slice (list_of_id):
func contains(s [] int, input int) bool {
for _, v := range s {
if v == input {
return true
}
}
return false
}
func main() {
list_of_id := [] int {1,2,3,4}
fmt.Println(contains(list_of_id, 1))
}
I want to create a function with the flexibility that I could input 1 or "1" as well.
My intention is to create an if else condition in which the slice of int [] int {1,2,3,4} will be converted into a slice of string [] string {"1","2","3","4"} to check again.
Anddddd, I don't know how to do so. I tried to google it out but all I found is a solution to convert this [] int {1,2,3,4} to this "{1,2,3,4}"
import "strconv"
func contains(s [] int, input interface{}) bool {
switch i := input.(type) {
case int:
for _, v := range s {
if v == i {
return true
}
}
case string:
for _, v := range s {
if strconv.Itoa(v) == i {
return true
}
}
}
return false
}
https://play.golang.org/p/02J1f77n_aM
package main
import (
"fmt"
"strconv"
)
func contains(s []int, input interface{}) bool {
var c int
var err error
switch input.(type) {
case int:
c = input.(int)
case string:
tmp := input.(string)
c, err = strconv.Atoi(tmp)
}
if err != nil {
return false
}
for _, v := range s {
if v == c {
return true
}
}
return false
}
func main() {
list_of_id := []int{1, 2, 3, 4}
fmt.Println(contains(list_of_id, 3))
fmt.Println(contains(list_of_id, 5))
fmt.Println(contains(list_of_id, "1"))
fmt.Println(contains(list_of_id, "6"))
}
Here example
Related
I have a string like this:
ports := []string{"1", "2-7", "12-1200", "10-500"}
I would like to make an integer set out of this like the output should be :
[]intSet{ 1, 2-7, 10-1200 }
Where intSet is some kind of integer set from which I am able to easily remove and add elements.
Update 1
intSet is a list of sets.
So, 2-7 is also a set.
Update 2
Here the largest set is merged.
e.g.
"1" -> 1
"2-7" -> 2-7
"12-1200" & "10-500" => "10..12.....500....1200" -> 10-1200
Since it's a set so it encompasses a unique range for this, a range which covers the whole set.
package main
import (
"fmt"
"log"
"strconv"
"strings"
)
type intSet struct {
start int
end int
}
func (s intSet) String() string {
if s.start == s.end {
return fmt.Sprintf("%d", s.start)
}
return fmt.Sprintf("%d-%d", s.start, s.end)
}
func (s intSet) in(i int) bool {
return s.start <= i && i <= s.end
}
func (s *intSet) union(set intSet) {
if set.start < s.start {
s.start = set.start
}
if set.end > s.end {
s.end = set.end
}
}
func insert(set intSet, is []intSet) bool {
for i, s := range is {
if s.in(set.start) || s.in(set.end) {
is[i].union(set)
return true
}
//updated here with thankful to #mh-cbon
if set.in(s.start) || set.in(s.end) {
is[i].union(set)
return true
}
}
return false
}
func main() {
var set []intSet
ports := []string{"1", "2-7", "12-1200", "10-500", "9-5500"}
for _, port := range ports {
s := strings.Split(port, `-`)
if len(s) < 1 || len(s) > 2 {
log.Fatalln(`set cannot have multiple values or no value`)
}
start, err := strconv.Atoi(s[0])
if err != nil {
log.Fatalln(err)
}
end := start
if len(s) == 2 {
end, err = strconv.Atoi(s[1])
if err != nil {
log.Fatalln(err)
}
}
temSet := intSet{
start: start,
end: end,
}
if !insert(temSet, set) {
set = append(set, temSet)
}
}
fmt.Println(set) //[1 2-7 9-5500]
}
run here
I have a struct with methods defined in it, i want to iterate through all the methods. Can i use interface {} or any other way to achieve this ?
type SomeStruct struct {
m_boolVals []bool
m_stringVals []string
m_intVals []int64
}
func (m *SomeStruct) GetBool() []bool {
return m.m_boolVals
}
func (m *SomeStruct) GetString() []string {
return m_stringVals
}
func (m *SomeStruct) GetInts() []int64 {
return m_intVals
}
Is there a way to achieve below code ? So basically only one of value would be present
fun SomeOtherFunc(ss *SomeStruct) []string {
var final_string []string
for _, handlerFunc := range(ss.GetBool, ss.GetString, ss.GetInts) {
generic_vals := handlerFunc()
if (len(generic_vals) > 0) {
for _, t_val := range(generic_vals) {
final_string = append(final_string , string(t_val))
}
break
}
}
return final_string
}
Here's an example how you can use reflection to iterate over the methods and call each, and convert their result to string:
func SomeOtherFunc(ss *SomeStruct) []string {
var result []string
v := reflect.ValueOf(ss)
for i := 0; i < v.NumMethod(); i++ {
for _, res := range v.Method(i).Call(nil) {
result = append(result, fmt.Sprint(res.Interface()))
}
}
return result
}
Testing it:
fmt.Println(SomeOtherFunc(&SomeStruct{
m_boolVals: []bool{true, false},
m_stringVals: []string{"one", "two"},
m_intVals: []int64{1, 2, 3},
}))
Which outputs (try it on the Go Playground):
[[true false] [1 2 3] [one two]]
I am trying to create a generic Binary Tree in Golang. How can I compare data from an interface and input data in the code? Here is an example of what I am trying to do. The comparison that is giving me trouble is this
} else if cur.data < data {
-
package DSAA
type TreeNode struct {
data interface{}
right *TreeNode
left *TreeNode
}
type BinarySearchTree struct {
root *TreeNode
}
func BSTCreate() *BinarySearchTree {
return &BinarySearchTree{nil}
}
func (b *BinarySearchTree) Insert(cur TreeNode, data interface{}) *BinarySearchTree {
if &cur == nil {
cur := &TreeNode{data, nil, nil}
} else if cur.data < data {
b = b.Insert(*cur.left, data)
} else {
b = b.Insert(*cur.right, data)
}
return b
}
You have some options:
1- Using runtime type switch:
package main
import (
"fmt"
)
func main() {
fmt.Println(Less(1, 2)) // true
fmt.Println(Less("AB", "AC")) // true
}
func Less(a, b interface{}) bool {
switch v := a.(type) {
case int:
w := b.(int)
return v < w
case string:
w := b.(string)
return v < w
}
return false
}
then replace } else if cur.data < data { with
} else if Less(cur.data , data) {
2- Using Comparer interface:
package main
import (
"fmt"
)
type Comparer interface {
// Less reports whether the element is less than b
Less(b interface{}) bool
}
func main() {
a, b := Int(1), Int(2)
fmt.Println(a.Less(b)) // true
c, d := St("A"), St("B")
fmt.Println(c.Less(d)) // true
}
type Int int
func (t Int) Less(b interface{}) bool {
if v, ok := b.(Int); ok {
return int(t) < int(v)
}
return false
}
type St string
func (t St) Less(b interface{}) bool {
if v, ok := b.(St); ok {
return string(t) < string(v)
}
return false
}
3- Using reflect
Let's say I want to write a function that finds a value in a slice
I intuitively want to write:
func find(s []interface{}, f func(interface{})bool) int {
for i, item := range s {
if f(item) {
return i
}
}
return -1
}
however I don't manage to do this with Go. I could have an interface with
Len() int
Value(int) interface{}
...
and this would work but in my real code things are more complicated (I need to do slices[from:end] etc), append, ... etc and if I redefine all this in an interface I end up having a lot of code. Is there a better way?
You can use reflection. I wrote this function for a project, feel free to use it:
// InSlice returns true if value is in slice
func InSlice(value, slice interface{}) bool {
switch reflect.TypeOf(slice).Kind() {
case reflect.Slice, reflect.Ptr:
values := reflect.Indirect(reflect.ValueOf(slice))
if values.Len() == 0 {
return false
}
val := reflect.Indirect(reflect.ValueOf(value))
if val.Kind() != values.Index(0).Kind() {
return false
}
for i := 0; i < values.Len(); i++ {
if reflect.DeepEqual(values.Index(i).Interface(), val.Interface()) {
return true
}
}
}
return false
}
if you have predefined type like []int or []string and do not want to convert to []interface{} see this working sample code (without using reflect):
package main
import "fmt"
func find(s []int, f func(int) bool) int {
for i, item := range s {
if f(item) {
return i
}
}
return -1
}
func findString(s []string, f func(string) bool) int {
for i, item := range s {
if f(item) {
return i
}
}
return -1
}
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(find(s, func(a int) bool { return a == 5 })) //5
strs := []string{"A", "B", "C"}
fmt.Println(findString(strs, func(a string) bool { return a == "B" })) //1
}
or you may use reflect,like this working sample code:
package main
import "fmt"
import "reflect"
func find(slice interface{}, f func(interface{}) bool) int {
switch reflect.TypeOf(slice).Kind() {
case reflect.Slice:
values := reflect.Indirect(reflect.ValueOf(slice))
for i := 0; i < values.Len(); i++ {
if f(values.Index(i).Interface()) {
return i
}
}
}
return -1
}
func main() {
a := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(find(a, func(i interface{}) bool { return i == 5 })) //5
b := []string{"A", "B", "C"}
fmt.Println(find(b, func(i interface{}) bool { return i == "B" })) //1
}
output:
5
1
I hope this helps.
I think, if you want to have slice of arbitrary values and use that sort of find function and have the possibility of standard [] reslicing, maybe the best way is to encapsulate your interface{} with another struct
type proxy struct {
val interface{}
}
and use
func find(s []proxy , f func(proxy)bool) int {}
and have the f function deal with interface{} comparison / type casting.
I am currently learning Golang, and i decided to write few simple algorithm for learning the syntax. i hope it's not already answers but i didn't found it ..
I have a problem for swapping string
func swap(str1, str2 string) {
/*
* Also possible :
* str1, str2 = str2, str1
*/
// str1, str2 = str2, str1
tmp := str1
str1 = str2
str2 = tmp
}
func main() {
a := "World !"
b := "Hello"
swap(a, b)
fmt.Printf("a=%s\nb=%s\n", a, b)
}
Why this code didn't work ?
Swapping str1 and str2 doesn't change a and b, because they are copies of a and b. Use pointers:
func swap(str1, str2 *string) {
*str1, *str2 = *str2, *str1
}
func main() {
a := "salut"
b := "les gens"
swap(&a, &b)
fmt.Printf("a=%s\nb=%s\n", a, b)
}
http://play.golang.org/p/Qw0t5I-XGT
This will be the idiomatic way.
package main
import "fmt"
func swap(a, b string)(string, string) {
return b, a
}
func main() {
f, s := swap("world" , "hello")
fmt.Println(f, s)
}
Built-in types as function arguments are passed by value, but if the elements for dereference will modify the original value, such as slice, map. e.g.
package main
import (
"bytes"
"fmt"
"strings"
)
func f_1(a int) {
a = 2
}
func f_1_1(a *int) {
*a = 2
}
func f_2(s string) {
s = "cba"
}
func f_2_1(s *string) {
*s = "cba"
}
func f_3(v []string) {
v[0] = "haha"
}
func f_3_1(v []string) {
v = nil
}
func f_3_2(v *[]string) {
*v = nil
}
func f_4(m map[int]int) {
m[1] = 3
m[3] = 1
}
func f_4_1(m map[int]int) {
m = nil
}
func f_4_2(m *map[int]int) {
*m = nil
}
func f_5(b []byte) {
b[0] = 0x40
}
func f_5_1(b []byte) {
b = bytes.Replace(b, []byte("1"), []byte("a"), -1)
}
func f_5_2(b *[]byte) {
*b = bytes.Replace(*b, []byte("1"), []byte("a"), -1)
}
type why struct {
s []string
}
func (ss why) SetV(s []string) {
ss.s = s
}
func (ss *why) SetP(s []string) {
ss.s = s
}
func (ss why) String() string {
return strings.Join(ss.s, ",")
}
func main() {
a := 1
s := "abc"
v := []string{"sd", "aa"}
m := map[int]int{1: 1, 2: 2, 3: 3}
f_1(a)
f_2(s)
f_3(v)
f_4(m)
fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
f_3_1(v)
f_4_1(m)
fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
f_1_1(&a)
f_2_1(&s)
f_3_2(&v)
f_4_2(&m)
fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
b := []byte("12145178")
f_5(b)
fmt.Printf("%s\n", b)
f_5_1(b)
fmt.Printf("%s\n", b)
f_5_2(&b)
fmt.Printf("%s\n", b)
ss := &why{}
ss.SetV([]string{"abc", "efg"})
fmt.Println(ss)
ss.SetP([]string{"abc", "efg"})
fmt.Println(ss)
}
you can write as that:
a := "hello"
b := "world"
a, b = b, a
package main
import "fmt"
func swap(a,b string)(string, string){
return b,a
}
func main(){
fmt.Println(swap("Lang","Go"))
}