小编A.D*_*A.D的帖子

将time.Time转换为字符串

我正在尝试将数据库中的一些值添加到[]stringGo中.其中一些是时间戳.

我收到错误:

不能使用U.Created_date(类型time.Time)作为数组元素中的类型字符串

我可以转换time.Timestring

type UsersSession struct {
    Userid int
    Timestamp time.Time
    Created_date time.Time
}

type Users struct {
    Name string
    Email string
    Country string
    Created_date time.Time
    Id int
    Hash string
    IP string
}
Run Code Online (Sandbox Code Playgroud)

-

var usersArray = [][]string{}

rows, err := db.Query("SELECT u.id, u.hash, u.name, u.email, u.country, u.IP, u.created_date, us.timestamp, us.created_date FROM usersSession AS us LEFT JOIN users AS u ON u.id = us.userid WHERE us.timestamp + interval 30 minute >= now()")

U …
Run Code Online (Sandbox Code Playgroud)

string time go

78
推荐指数
3
解决办法
14万
查看次数

Golang:逃避单引号

有没有办法逃避单引号?

下列:

str := "I'm Bob, and I'm 25."
str = strings.Replace(str, "'", "\'", -1)
Run Code Online (Sandbox Code Playgroud)

给出错误:未知的转义序列:'

我想成为

"I\'m Bob, and I\'m 25."
Run Code Online (Sandbox Code Playgroud)

escaping go backslash quote

9
推荐指数
2
解决办法
3万
查看次数

Perl - 逗号上的拆分字符串.忽略空格

我有这个字符串:

$str="     a, b,    c>d:e,  f,    g ";
Run Code Online (Sandbox Code Playgroud)

在此字符串中可能有空格和/或制表符

我在perl中拆分了字符串:

my (@COLUMNS) = split(/[\s\t,]+/, $str));
Run Code Online (Sandbox Code Playgroud)

但这会在位置[0]中创建一个领先的空间.

@COLUMNS=[

    a
    b
    c>d:e
    f
    g
]
Run Code Online (Sandbox Code Playgroud)

我要这个:

@COLUMNS=[
    a
    b
    c>d:e
    f
    g
]
Run Code Online (Sandbox Code Playgroud)

regex perl whitespace split comma

3
推荐指数
2
解决办法
2302
查看次数

使用 preloadedState 在 React 中进行测试:在函数中手动导入存储时,redux 存储会不同步

我正在使用react-testing-library和jest为React编写测试,并且在弄清楚当另一个文件导入存储时如何为我的redux存储设置preloadedState时遇到一些问题。

我有一个功能可以像这样设置我的商店

商店.ts

  export const history = createBrowserHistory()
  export const makeStore = (initalState?: any) => configureStore({
    reducer: createRootReducer(history),
    preloadedState: initalState
  })
  export const store = makeStore()
Run Code Online (Sandbox Code Playgroud)

和另一个像这样的js文件

实用程序.ts

  import store from 'state/store'
  const userIsDefined = () => {
    const state = store.getState()
    if (state.user === undefined) return false
    ...
    return true
  }
Run Code Online (Sandbox Code Playgroud)

然后我有一个看起来像这样的测试:

utils.test.tsx( renderWithProvider 基本上是一个渲染函数,它也将我的组件包装在渲染组件中,请参阅: https: //redux.js.org/recipes/writing-tests#connected-components

  describe("Test", () => {
    it("Runs the function when user is defined", async () => {
      const store = makeStore({ user: …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs jestjs redux react-testing-library

3
推荐指数
1
解决办法
2538
查看次数