10 examples of 'golang foreach' in Go

Every line of 'golang foreach' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
33func (*Headers) ForEach(callback func(keyId []byte, status *mediakeystatus.MediaKeyStatus)) {
34 macro.Rewrite("$_.forEach($1)", callback)
35}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

76func (node *TrieNode) ForEach(callback func(*TrieNode)) {
77 callback(node)
78 for _, child := range node.children {
79 child.ForEach(callback)
80 }
81}
888func (lq *LinqObj) ForEach(line string, scope *Scope, args ...Object) Object {
889 if len(args) != 1 {
890 return NewError(line, ARGUMENTERROR, "1", len(args))
891 }
892
893 block, ok := args[0].(*Function)
894 if !ok {
895 return NewError(line, PARAMTYPEERROR, "first", "forEach", "*Function", args[0].Type())
896 }
897
898 s := NewScope(scope, nil)
899
900 next := lq.Query.Iterate()
901
902 for item, ok := next(); ok.Bool; item, ok = next() {
903 s.Set(block.Literal.Parameters[0].(*ast.Identifier).Value, item)
904 Eval(block.Literal.Body, s)
905 }
906
907 return NIL
908}
160func ForEach(in, fn interface{}) {
161 sliceForEach(
162 reflect.ValueOf(in),
163 reflect.ValueOf(fn),
164 )
165}
198func (t Term) ForEach(args ...interface{}) Term {
199 return constructMethodTerm(t, "Foreach", p.Term_FOREACH, funcWrapArgs(args), map[string]interface{}{})
200}
368func (file File) ForEach(callback func(line, value int)) {
369 for iter := file.tree.Min(); !iter.Limit(); iter = iter.Next() {
370 item := iter.Item()
371 key := int(item.Key)
372 var value int
373 if item.Value == math.MaxUint32 {
374 value = -1
375 } else {
376 value = int(item.Value)
377 }
378 callback(key, value)
379 }
380}
119func (p *pathObject) ForEach(call otto.FunctionCall) otto.Value {
120 it := p.buildIteratorTree()
121 it.Tagger().Add(TopResultTag)
122 limit := -1
123 if len(call.ArgumentList) == 0 {
124 return otto.NullValue()
125 }
126 callback := call.Argument(len(call.ArgumentList) - 1)
127 args := exportArgs(call.ArgumentList[:len(call.ArgumentList)-1])
128 if len(args) > 0 {
129 limit = toInt(args[0])
130 }
131 p.wk.runIteratorWithCallback(it, callback, call, limit)
132 return otto.NullValue()
133}
43func (m *memRegistryDB) Foreach(callback func(controllerID, address string) bool) {
44 m.mutex.Lock()
45 defer m.mutex.Unlock()
46
47 for controllerID, address := range m.db {
48 if !callback(controllerID, address) {
49 return
50 }
51 }
52}
306func (cnt *htmlContents) forEach(it func(i int, line *htmlLine)) {
307 for i, l := range cnt.lines {
308 // skip unsupported tags
309 if _, ok := cnt.htmlTagWeights[l.tag.String()]; !ok {
310 continue
311 }
312 it(i, l)
313 }
314}
62func (cli *hostIterator) ForEach(itemCallback func(item HostInterface) error) (err error) {
63 var item HostInterface
64 for {
65
66 item, err = cli.Next()
67 if nil != err {
68 if io.EOF == err {
69 err = nil
70 }
71 break
72 }
73
74 if nil == item {
75 break
76 }
77
78 err = itemCallback(item)
79 if nil != err {
80 break
81 }
82 }
83 cli.bufIdx = 0
84 return err
85}

Related snippets