{"id":1810,"date":"2021-09-09T15:01:20","date_gmt":"2021-09-09T06:01:20","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=1810"},"modified":"2021-09-15T15:12:52","modified_gmt":"2021-09-15T06:12:52","slug":"golang1-17","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2021\/09\/09\/golang1-17\/","title":{"rendered":"Go1.17\u306e\u65b0\u6a5f\u80fd\u3092\u5c0e\u5165\u3057\u307e\u3057\u305f"},"content":{"rendered":"

\u3053\u3093\u306b\u3061\u306f\u3002\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u7267\u7530\u3067\u3059\u3002\u4eca\u56de\u306f\u307f\u3093\u306a\u5927\u597d\u304dGo\u306b\u3064\u3044\u3066\u3067\u3059\u3002<\/p>\n

Go1.17\u30ea\u30ea\u30fc\u30b9\uff01<\/h2>\n

\u5148\u6708Go1.17\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u307e\u3057\u305f\uff01\u81ea\u5206\u306fGo\u3092\u4f7f\u3044\u59cb\u3081\u3066\u307e\u3060\u534a\u5e74\u307b\u3069\u306a\u306e\u3067\u3001\u4eca\u56de\u304c\u521d\u3081\u3066\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u306e\u7d4c\u9a13\u306b\u306a\u308a\u307e\u3057\u305f\u304c\u3001\u3088\u304f\u4f7f\u3063\u3066\u3044\u308b\u8a00\u8a9e\u306b\u65b0\u3057\u3044\u6a5f\u80fd\u304c\u8ffd\u52a0\u3055\u308c\u308b\u3068\u306a\u308b\u3068\u3001\u3084\u306f\u308a\u3068\u3066\u3082\u6c17\u306b\u306a\u308a\u307e\u3059\u306d\u3002<\/p>\n

\u30ea\u30ea\u30fc\u30b9\u30ce\u30fc\u30c8\u306f\u3053\u3061\u3089<\/a>\u3067\u3059\u3002Go1.17\u3067\u306f\u5168\u4f53\u7684\u306b\u7d30\u304b\u3044\u6a5f\u80fd\u306e\u8ffd\u52a0\u304c\u591a\u304f\u3001\u305d\u3053\u307e\u3067\u76ee\u7389\u6a5f\u80fd\u306e\u3088\u3046\u306a\u3082\u306e\u306f\u3042\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001\u3044\u304f\u3064\u304b\u4fbf\u5229\u306a\u6a5f\u80fd\u3082\u8ffd\u52a0\u3055\u308c\u3066\u3044\u305f\u306e\u3067\u3001\u73fe\u5728\u958b\u767a\u4e2d\u306e\u30d7\u30ed\u30c0\u30af\u30c8\u306b\u3082\u5c0e\u5165\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u307e\u305f\u3001\u6a5f\u80fd\u306e\u4ed6\u306b\u3082\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u6539\u5584\u304c\u306a\u3055\u308c\u3066\u304a\u308a\u3001Go1.17\u306b\u5909\u3048\u308b\u3060\u3051\u3067\u7d045%\u9ad8\u901f\u5316\u3057\u305f\u3068\u3044\u3046\u306e\u3082\u5b09\u3057\u3044\u3068\u3053\u308d\u3067\u3059\u3002\uff08https:\/\/golang.org\/doc\/go1.17#compiler<\/a>\uff09<\/p>\n

\u5b9f\u969b\u306b\u5c0e\u5165\u3057\u305f\u6a5f\u80fd<\/h2>\n

\u3053\u3053\u304b\u3089\u306f\u3001\u73fe\u5728\u958b\u767a\u4e2d\u306e\u30d7\u30ed\u30c0\u30af\u30c8\u306b\u5b9f\u969b\u306b\u53d6\u308a\u5165\u308c\u305f\u65b0\u6a5f\u80fd\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002\u53d6\u308a\u5165\u308c\u305f\u6a5f\u80fd\u306f\u3001\u4e3b\u306b\u30c6\u30b9\u30c8\u95a2\u9023\u306b\u3064\u3044\u3066\u3067\u3059\u3002<\/p>\n

T.Setenv<\/h3>\n
\n

Setenv calls os.Setenv(key, value) and uses Cleanup to restore the environment variable to its original value after the test.
\nThis cannot be used in parallel tests.<\/p>\n<\/blockquote>\n

T.Setenv\u306f\u3001\u30c6\u30b9\u30c8\u4e2d\u306b\u4e00\u6642\u7684\u306b\u74b0\u5883\u5909\u6570\u3092\u8a2d\u5b9a\u3067\u304d\u308b\u6a5f\u80fd\u3067\u3059\uff08\u30d9\u30f3\u30c1\u30de\u30fc\u30af\u4e2d\u306b\u540c\u69d8\u306b\u6a5f\u80fd\u3059\u308bB.Setenv\u00a0\u3082\u8ffd\u52a0\u3055\u308c\u3066\u3044\u307e\u3059\uff09\u3002\u3064\u307e\u308a\u3001\u5143\u304b\u3089\u74b0\u5883\u5909\u6570\u304c\u30bb\u30c3\u30c8\u3055\u308c\u3066\u3044\u305f\u5834\u5408\u306f\u3001\u30c6\u30b9\u30c8\u4e2d\u3060\u3051\u6307\u5b9a\u3057\u305f\u5024\u306b\u5207\u308a\u66ff\u308f\u308a\u3001\u30c6\u30b9\u30c8\u304c\u7d42\u308f\u3063\u305f\u3089\u5143\u306e\u5024\u306b\u623b\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n

\u5185\u90e8\u306e\u5b9f\u88c5\u306f\u2193\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u304a\u308a\u3001os.Setenv<\/code>\u3067\u74b0\u5883\u5909\u6570\u3092\u6307\u5b9a\u3057\u305f\u5024\u306b\u66f8\u304d\u63db\u3048\u3066\u3001\u30c6\u30b9\u30c8\u7d42\u4e86\u6642\u306b\u5143\u306e\u5024\u306b\u66f8\u304d\u63db\u3048\u3066\u3044\u307e\u3059\u3002\u4eca\u307e\u3067\u306f\u81ea\u5206\u3067\u5b9f\u88c5\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u5c11\u3057\u4fbf\u5229\u306b\u306a\u308a\u307e\u3057\u305f\u306d\u3002<\/p>\n

https:\/\/cs.opensource.google\/go\/go\/+\/go1.17:src\/testing\/testing.go;l=1120<\/a><\/p>\n

func (c *common) Setenv(key, value string) {\n    prevValue, ok := os.LookupEnv(key)\n\n    if err := os.Setenv(key, value); err != nil {\n        c.Fatalf("cannot set environment variable: %v", err)\n    }\n\n    if ok {\n        c.Cleanup(func() {\n            os.Setenv(key, prevValue)\n        })\n    } else {\n        c.Cleanup(func() {\n            os.Unsetenv(key)\n        })\n    }\n}<\/code><\/pre>\n

\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u3082\u66f8\u304b\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001t.Parallel\u3068\u540c\u6642\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n

\u30c6\u30b9\u30c8\u306eShuffle\u6a5f\u80fd<\/h3>\n
\n

-shuffle off,on,N
\nRandomize the execution order of tests and benchmarks.
\nIt is off by default. If -shuffle is set to on, then it will seed
\nthe randomizer using the system clock. If -shuffle is set to an
\ninteger N, then N will be used as the seed value. In both cases,
\nthe seed will be reported for reproducibility.<\/p>\n<\/blockquote>\n

\u30c6\u30b9\u30c8\u306eShuffle\u6a5f\u80fd\u3092\u4f7f\u3046\u3068\u3001\u8907\u6570\u30c6\u30b9\u30c8\u306e\u5b9f\u884c\u9806\u5e8f\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002go test -shuffle=on<\/code>\u306e\u3088\u3046\u306b\u3057\u3066\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff08\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u30aa\u30d5\u306b\u306a\u3063\u3066\u3044\u307e\u3059\uff09\u3002<\/p>\n

\u5b9f\u884c\u9806\u5e8f\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u3082\u3057\u9806\u5e8f\u306b\u4f9d\u5b58\u3059\u308b\u30c6\u30b9\u30c8\u304c\u66f8\u304b\u308c\u3066\u3044\u305f\u5834\u5408\u3001\u305d\u308c\u3092\u691c\u77e5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u9806\u5e8f\u306b\u4f9d\u5b58\u3059\u308b\u30c6\u30b9\u30c8\u306f\u3001\u4f8b\u3048\u3070\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u524d\u306e\u30c6\u30b9\u30c8\u3067\u4f7f\u3063\u305f\u30c7\u30fc\u30bf\u304c\u6b8b\u3063\u3066\u3044\u305f\u308a\u3001\u524d\u306e\u30c6\u30b9\u30c8\u3067\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u304c\u5909\u66f4\u3055\u308c\u3066\u3044\u305f\u308a\u3059\u308b\u5834\u5408\u306b\u8d77\u3053\u308a\u307e\u3059\u3002\u672c\u6765\u306f\u305d\u3046\u306a\u3089\u306a\u3044\u3088\u3046\u306b\u6c17\u3092\u3064\u3051\u308b\uff08\u305d\u3046\u306a\u3089\u306a\u3044\u4ed5\u7d44\u307f\u3092\u4f5c\u308b\uff09\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3082\u3057\u305d\u3046\u306a\u3063\u3066\u3044\u305f\u5834\u5408\u306b\u3082\u6c17\u3065\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n

T.Cleanup\uff08Go1.17\u306e\u65b0\u6a5f\u80fd\u3067\u306f\u306a\u3044\u304c\u3001\u65b0\u3057\u304f\u5c0e\u5165\u3057\u305f\uff09<\/h3>\n

\u4eca\u56de\u30c6\u30b9\u30c8\u306b\u95a2\u3059\u308b\u65b0\u3057\u3044\u6a5f\u80fd\u3092\u53d6\u308a\u5165\u308c\u308b\u306b\u3042\u305f\u3063\u3066\u8272\u3005\u8abf\u3079\u3066\u3044\u305f\u3068\u3053\u308d\u3001Go1.14\u3067T.Cleanup\u3068\u3044\u3046\u6a5f\u80fd\u304c\u8ffd\u52a0\u3055\u308c\u3066\u304a\u308a\u3001\u3053\u308c\u304c\u4e0a\u624b\u304f\u4f7f\u3048\u305d\u3046\u3060\u3063\u305f\u306e\u3067\u5c0e\u5165\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u304a\u6c17\u3065\u304d\u306e\u65b9\u3082\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3053\u308c\u306fT.Setenv\u306e\u5185\u90e8\u3067\u4f7f\u308f\u308c\u3066\u3044\u308b\u6a5f\u80fd\u3067\u3059\uff08\u81ea\u5206\u306f\u5f8c\u3067\u6c17\u3065\u304d\u307e\u3057\u305f\u2026\uff09\u3002<\/p>\n

T.Cleanup\u3092\u4f7f\u3046\u3068\u3001\u30c6\u30b9\u30c8\u7d42\u4e86\u6642\u306b\u5b9f\u884c\u3059\u308b\u95a2\u6570\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002defer\u3092\u4f7f\u3046\u306e\u3068\u540c\u3058\u3058\u3083\u306a\u3044\u306e\uff1f\u3068\u601d\u3046\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u305d\u308c\u3088\u308a\u4fbf\u5229\u306b\u306a\u308b\u5834\u9762\u3082\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u8907\u6570\u306e\u30c6\u30b9\u30c8\u3067\u4f7f\u3046\u5171\u901a\u306e\u95a2\u6570\u3067\u4e0a\u624b\u304f\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

\u5b9f\u969b\u306b\u4f7f\u7528\u3057\u305f\u4f8b\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002\u3053\u306e\u4f8b\u3067\u306f\u3001\u30c6\u30b9\u30c8\u306e\u6700\u521d\u306b\u30c6\u30b9\u30c8\u7528\u306eDB\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u3066\u3001\u30c6\u30b9\u30c8\u7d42\u4e86\u6642\u306bDB\u3092\u524a\u9664\u3059\u308b\u95a2\u6570\u3092\u4f5c\u3063\u3066\u3044\u307e\u3059\uff08\u3053\u306e\u95a2\u6570\u3092\u3044\u308d\u3093\u306a\u30c6\u30b9\u30c8\u306e\u6700\u521d\u306b\u4f7f\u3044\u56de\u3059\uff09\u3002<\/p>\n

\u4eca\u307e\u3067\u306edefer\u3092\u7528\u3044\u305f\u5834\u5408\u306f\u2193\u306e\u3088\u3046\u306a\u611f\u3058\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

func SetupDB(t *testing.T) (*sql.DB, func()) {\n    \/\/ DB\u3092\u4f5c\u6210\n    \/\/ db, err := sql.Open("mysql", ...)\n    \/\/ db.Exec("CREATE DATABASE xxx")\n\n    return db, func() {\n        db.Exec("DROP DATABASE xxx") \/\/ DB\u3092\u524a\u9664\n    }\n}\n\nfunc Test_main(t *testing.T) {\n    db, clearDB := SetupDB(t)\n    defer clearDB()\n\n    \/\/ db\u3092\u4f7f\u3063\u3066\u30c6\u30b9\u30c8\n}\n<\/code><\/pre>\n

\u3053\u308c\u3092t.Cleanup\u3092\u4f7f\u3046\u3068\u2193\u306e\u3088\u3046\u306b\u66f8\u3051\u307e\u3059\u3002<\/p>\n

func SetupDB(t *testing.T) *sql.DB {\n    \/\/ DB\u3092\u4f5c\u6210\n    \/\/ db, err := sql.Open("mysql", ...)\n    \/\/ db.Exec("CREATE DATABASE xxx")\n\n    t.Cleanup(func() {\n      db.Exec("DROP DATABASE xxx")\n    })\n\n    return db\n}\n\nfunc Test_main(t *testing.T) {\n    db := SetupDB(t)\n\n    \/\/ db\u3092\u4f7f\u3063\u3066\u30c6\u30b9\u30c8\n}<\/code><\/pre>\n

\u4eca\u307e\u3067\u306f\u3001defer\u7528\u306e\u95a2\u6570\u3092\u8fd4\u3057\u3066\u30c6\u30b9\u30c8\u672c\u4f53\u306e\u95a2\u6570\u3067\u5b9f\u884c\u3057\u306a\u3051\u308c\u3070\u3044\u3051\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001t.Cleanup\u3092\u4f7f\u3046\u3053\u3068\u3067\u305d\u308c\u304c\u5fc5\u8981\u306a\u304f\u306a\u308a\u307e\u3057\u305f\u3002\u7d50\u69cb\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n

\u307e\u3068\u3081<\/h2>\n

\u4eca\u56de\u306fGo1.17\u3067\u8ffd\u52a0\u3055\u308c\u305f\u65b0\u6a5f\u80fd\u306e\u4e00\u90e8\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u4eca\u56de\u7d39\u4ecb\u3057\u305f\u4ed6\u306b\u3082\u8272\u3005\u6a5f\u80fd\u304c\u8ffd\u52a0\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u6c17\u306b\u306a\u3063\u305f\u65b9\u306f\u8abf\u3079\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002Go1.18\u3067\u306f\u30b8\u30a7\u30cd\u30ea\u30af\u30b9\u304c\u8ffd\u52a0\u3055\u308c\u308b\u3068\u3044\u3046\u8a71\u306a\u306e\u3067\u3001\u3068\u3066\u3082\u697d\u3057\u307f\u3067\u3059\u3002\u6b21\u56de\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u3082\u697d\u3057\u307f\u306b\u3057\u3066\u307e\u3059\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u3053\u3093\u306b\u3061\u306f\u3002\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u7267\u7530\u3067\u3059\u3002\u4eca\u56de\u306f\u307f\u3093\u306a\u5927\u597d\u304dGo\u306b\u3064\u3044\u3066\u3067\u3059\u3002 Go1.17\u30ea\u30ea\u30fc\u30b9\uff01 \u5148\u6708Go1.17\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u307e\u3057\u305f\uff01\u81ea\u5206\u306fGo\u3092\u4f7f\u3044\u59cb\u3081\u3066\u307e\u3060\u534a\u5e74\u307b\u3069\u306a\u306e\u3067\u3001\u4eca\u56de\u304c\u521d\u3081\u3066\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u306e\u7d4c\u9a13\u306b\u306a\u308a\u307e\u3057\u305f\u304c\u3001\u3088\u304f\u4f7f\u3063\u3066\u3044\u308b\u8a00\u8a9e\u306b\u65b0\u3057\u3044\u6a5f\u80fd\u304c\u8ffd\u52a0\u3055\u308c\u308b\u3068\u306a\u308b\u3068\u3001\u3084\u306f\u308a\u3068\u3066\u3082\u6c17\u306b\u306a\u308a\u307e\u3059\u306d\u3002 \u30ea\u30ea\u30fc\u30b9\u30ce\u30fc\u30c8\u306f\u3053\u3061\u3089\u3067\u3059\u3002Go1.17\u3067\u306f\u5168\u4f53\u7684\u306b\u7d30\u304b\u3044\u6a5f\u80fd\u306e\u8ffd\u52a0\u304c\u591a\u304f\u3001\u305d\u3053\u307e\u3067\u76ee\u7389\u6a5f […]<\/p>\n","protected":false},"author":12,"featured_media":1813,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[128],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/1810"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=1810"}],"version-history":[{"count":3,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/1810\/revisions"}],"predecessor-version":[{"id":1814,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/1810\/revisions\/1814"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/1813"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=1810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=1810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=1810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}