在golang中将Json.Number转换为int / int64 / float64

时间:2018-01-25 12:59:54

标签: go go-interface

我有一个可变数据,这是一个接口。当我打印它的类型时,我把它作为json.Number。如何输入强制转换为int / int64 / float64

如果我尝试数据。(float64),它最终会出现恐慌错误

panic: interface conversion: interface {} is json.Number, not float64

1 个答案:

答案 0 :(得分:3)

查看此文档以了解MongoClient { domain: null, _events: {}, _eventsCount: 0, _maxListeners: undefined, s: { url: 'mongodb://localhost:27017/BotDb', options: { socketOptions: {}, read_preference_tags: null, readPreference: [Object], dbName: 'slackBotDb', servers: [Object], server_options: [Object], db_options: [Object], rs_options: [Object], mongos_options: [Object], socketTimeoutMS: 360000, connectTimeoutMS: 30000, promiseLibrary: [Function: Promise] }, promiseLibrary: [Function: Promise], dbCache: {}, sessions: [] }, topology: Server { domain: null, _events: { serverOpening: [Function], serverDescriptionChanged: [Function], serverHeartbeatStarted: [Function], serverHeartbeatSucceeded: [Function], serverHeartbeatFailed: [Function], serverClosed: [Function], topologyOpening: [Function], topologyClosed: [Function], topologyDescriptionChanged: [Function], joined: [Function], left: [Function], ping: [Function], ha: [Function], authenticated: [Function], error: [Function], timeout: [Function], close: [Function], parseError: [Function], open: [Object], fullsetup: [Object], all: [Object], reconnect: [Function] }, _eventsCount: 22, _maxListeners: undefined, clientInfo: { driver: [Object], os: [Object], platform: 'Node.js v7.10.0, LE' }, s: { coreTopology: [Object], sCapabilities: null, clonedOptions: [Object], reconnect: true, emitError: true, poolSize: 5, storeOptions: [Object], store: [Object], host: 'localhost', port: 27017, options: [Object], sessionPool: [Object], promiseLibrary: [Function: Promise] } } } 上的可用方法: https://golang.org/pkg/encoding/json/#Number

> db.getCollection("users");
BotDb.users