V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
252748371

切片如何转数组?

  •  
  •   252748371 · Sep 25, 2019 · 4376 views
    This topic created in 2406 days ago, the information mentioned may be changed or developed.

    切片如何转数组? gorm 的 in 查询不支持切片

    9 replies    2019-11-15 17:24:12 +08:00
    codehz
        1
    codehz  
       Sep 25, 2019
    首先必须长度固定,然后
    var target[5]int
    copy(target[:], source)
    252748371
        2
    252748371  
    OP
       Sep 25, 2019
    @codehz
    问题是长度不知道啊
    codehz
        3
    codehz  
       Sep 25, 2019
    那就
    target := make([]int, n)
    copy(target[:], source)
    chotow
        4
    chotow  
       Sep 25, 2019
    gorm 的 in 查询是支持切片的: http://gorm.io/docs/query.html#Plain-SQL
    xorm 不支持(吐血)
    252748371
        6
    252748371  
    OP
       Sep 25, 2019
    @chotow
    哥 帮我看看
    sql: converting argument $4 type: unsupported type []string, a slice of string
    252748371
        7
    252748371  
    OP
       Sep 25, 2019
    解决了
    原来不能传指针!
    chotow
        8
    chotow  
       Sep 27, 2019
    @kedadiannao220 #5
    xorm Raw SQL 查询的时候,怎么实现 in 传值呢?
    我试了下:db.SQL("select * from test").In("id", []uint64{1, 2, 3}).Find(&ret)
    这样子并不可以
    kedadiannao220
        9
    kedadiannao220  
       Nov 15, 2019
    @chotow
    使用 raw sql,就不用再使用 In、where 这些函数了;
    db.SQL("select * from test where id in xxxx"),这样会存在 sql 注入的风险
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3146 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 14:32 · PVG 22:32 · LAX 07:32 · JFK 10:32
    ♥ Do have faith in what you're doing.