0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScriptのforEachをGoで再現する

Posted at

Goでは標準でJavaScriptのforEachのような配列の中身をコールバック関数で操作する機能が無いため自力で実装する必要がある。

今回再現する元のJavaScriptのコード

元のコード

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

Goで再現したコード

rangeの中身即時関数を配置する形で再現した。

package main

import "fmt"

func main() {
	array1 := [3]string{"a", "b", "c"}

	for _, v := range array1 {
		func(element string) {
			fmt.Println(element)
		}(v)
	}
}

余談

rangeの中身即時関数を配置するのなら、そのままrangeに処理を書けばいい話なるな.....、

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?