github.com/gogf/gf@v1.16.9/util/gutil/gutil_z_unit_struct_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gutil_test 8 9 import ( 10 "github.com/gogf/gf/frame/g" 11 "testing" 12 13 "github.com/gogf/gf/test/gtest" 14 "github.com/gogf/gf/util/gutil" 15 ) 16 17 func Test_StructToSlice(t *testing.T) { 18 type A struct { 19 K1 int 20 K2 string 21 } 22 gtest.C(t, func(t *gtest.T) { 23 a := &A{ 24 K1: 1, 25 K2: "v2", 26 } 27 s := gutil.StructToSlice(a) 28 t.Assert(len(s), 4) 29 t.AssertIN(s[0], g.Slice{"K1", "K2", 1, "v2"}) 30 t.AssertIN(s[1], g.Slice{"K1", "K2", 1, "v2"}) 31 t.AssertIN(s[2], g.Slice{"K1", "K2", 1, "v2"}) 32 t.AssertIN(s[3], g.Slice{"K1", "K2", 1, "v2"}) 33 }) 34 gtest.C(t, func(t *gtest.T) { 35 s := gutil.StructToSlice(1) 36 t.Assert(s, nil) 37 }) 38 }