Created
January 26, 2023 01:47
Revisions
-
heiwa4126 revised this gist
Jan 26, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ package main import "fmt" // ChatGPTに // 「Go言語で配列を逆にする関数を書いてください」(この例ではスライスだけど) // と聞いて出てきたコードはコンパイルできなかった。 // - 出力のとこを変えた // - gerenicsにしてみた (go 1.8以上) -
heiwa4126 created this gist
Jan 26, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ package main import "fmt" // ChatGPTに // 「Go言語で配列を逆にする関数を書いてください」 // と聞いて出てきたコードはコンパイルできなかった。 // - 出力のとこを変えた // - gerenicsにしてみた (go 1.8以上) func reverse[T interface{}](s []T) { for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { s[i], s[j] = s[j], s[i] } } func main() { a := []int{1, 2, 3, 4, 5} fmt.Printf("%#v\n", a) reverse(a) fmt.Printf("%#v\n", a) b := []string{"apple", "banana", "cherry"} fmt.Printf("%#v\n", b) reverse(b) fmt.Printf("%#v\n", b) }