小编Nic*_*all的帖子

Golang:不同结构类型之间的转换是否可能?

假设我有两种类似的类型:

type type1 []struct {
    Field1 string
    Field2 int
}
type type2 []struct {
    Field1 string
    Field2 int
}
Run Code Online (Sandbox Code Playgroud)

是否有直接的方法将值从type1写入type2,知道它们具有相同的字段?(除了编写一个循环,将所有字段从源复制到目标)

谢谢.

struct go

46
推荐指数
7
解决办法
6万
查看次数

React Formik:如何使用自定义onChange和onBlur

我开始使用formik库进行反应,我无法弄清楚道具handleChange和handleBlur的用法.

根据文档,handleBlur可以设置为a上的prop <Formik/>,然后必须手动向下传递给<input/>.

我已经尝试过了,没有成功:(为了更清晰,我保留了关于handleBlur的代码)

import React from "react";
import { Formik, Field, Form } from "formik";
import { indexBy, map, compose } from "ramda";
import { withReducer } from "recompose";

const MyInput = ({ field, form, handleBlur, ...rest }) =>
  <div>
    <input {...field} onBlur={handleBlur} {...rest} />
    {form.errors[field.name] &&
      form.touched[field.name] &&
      <div>
        {form.errors[field.name]}
      </div>}
  </div>;

const indexById = indexBy(o => o.id);
const mapToEmpty = map(() => "");

const EmailsForm = ({ fieldsList }) => …
Run Code Online (Sandbox Code Playgroud)

javascript forms reactjs

27
推荐指数
1
解决办法
2万
查看次数

转到模板:覆盖结构切片而不是切片结构

稍微使用了Golang的HTML模板,我发现在模板中循环对象的所有示例都是将切片的结构传递给模板,有点像这个示例:

type UserList struct {
    Id   []int
    Name []string
}

var templates = template.Must(template.ParseFiles("main.html"))

func rootHandler(w http.ResponseWriter, r *http.Request) {
    users := UserList{
        Id:   []int{0, 1, 2, 3, 4, 5, 6, 7},
        Name: []string{"user0", "user1", "user2", "user3", "user4"},
    }
    templates.ExecuteTemplate(w, "main", &users)
}
Run Code Online (Sandbox Code Playgroud)

使用"主"模板:

{{define "main"}}
    {{range .Name}}
        {{.}}
    {{end}}
{{end}}
Run Code Online (Sandbox Code Playgroud)

这是有效的,但我不明白如果我只在.Name属性上,我应该如何显示其相应名称旁边的每个ID.我会发现将每个用户视为一个对象,在显示时对其属性进行分组更合乎逻辑.

因此我的问题:

现在,如果我想将一片结构传递给模板怎么办?使这项工作的语法是什么?我还没有找到或理解如何在官方的html /模板文档中.我想象这样的东西像这样:

type User struct {
    Id   int
    Name string
}
type UserList []User
var myuserlist UserList = ...
Run Code Online (Sandbox Code Playgroud)

和模板看起来有点像这样:(这里的语法故意错,只是为了理解)

{{define "main"}}
    {{for each User from …
Run Code Online (Sandbox Code Playgroud)

html templates struct go go-templates

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

Golang模板:如何在变量中定义数组?

在go模板中定义数组变量的正确语法是什么?(这里是一个HTML模板).这是我试过的:

{{define "template"}}
    {{ $x:=[]int{0,1,2} }}{{$x[0]}}
{{end}}
Run Code Online (Sandbox Code Playgroud)

错误日志说: unexpected "[" in command

谢谢.

arrays variables templates go

11
推荐指数
2
解决办法
1万
查看次数

Git钩子:应用`git config core.hooksPath`

我有一个带有预提交钩子的git存储库:

my-repo
|- .git
   |- hooks
      |- pre-commit     # I made this file executable
Run Code Online (Sandbox Code Playgroud)

一直到那里,一切正常.我提交时挂钩正在运行.

=================================

我现在跑git config core.hooksPath ./git-config/hooks进去了my-repo.

文件夹结构是这样的:

my-repo
|- .git
   |- hooks
|- git-config
   |- hooks
      |- pre-commit     # I made this file executable as well
Run Code Online (Sandbox Code Playgroud)

会发生什么:

  • 新的预提交脚本不会在提交时运行
  • 如果我保留它,旧的预提交脚本仍会在提交时运行 my-repo/.git/hooks
  • git config --get core.hooksPathmy-repo输出中运行./git-config/hooks

如何在提交时运行新的预提交钩子?

以下是我显然不太了解的文档的链接:
https://git-scm.com/docs/git-config
https://git-scm.com/docs/githooks

git githooks

10
推荐指数
1
解决办法
1万
查看次数

如何在结构中存储异步函数并从结构实例调用它?

我正在尝试使用新的async/await语法、std::future::Futures 和 Tokio 的最新版本来实现这一点。我正在使用 Tokio0.2.0-alpha.4和 Rust 1.39.0-nightly

我尝试过的不同的事情包括:

  • Box<dyn>s 用于我想存储在结构体中的类型
  • 在结构定义中使用泛型

我无法得到一个最小的工作版本,所以这是我想要实现的简化版本:

async fn foo(x: u8) -> u8 {
    2 * x
}

// type StorableAsyncFn = Fn(u8) -> dyn Future<Output = u8>;

struct S {
    f: StorableAsyncFn,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let s = S { f: foo };

    let out = (s.f)(1).await;

    Ok(())
}
Run Code Online (Sandbox Code Playgroud)

当然,此代码无法编译并出现以下错误:

error[E0412]: cannot …
Run Code Online (Sandbox Code Playgroud)

struct asynchronous future rust async-await

10
推荐指数
2
解决办法
4177
查看次数

使用模板内的条件

在模板中使用if语句确实令我感到困惑.

我正在尝试class = "active"使用golang模板创建一个导航列表,以执行检测活动选项卡的基本选项卡菜单.这是我的尝试:

{{define "header"}}
<!DOCTYPE html>
<html>
    <head>
        <title>Geoprod</title>
        {{template "stylesheet" .}}
    </head>
    <body>
        <nav class="navbar" role="navigation">
          <div class="navbar-header">
            <a{{if eq .Active "accueil"}} class="active"{{end}} href="/">Geoprod</a>
          </div>
          <div class="navbar-body">
            <ul class="navbar-list">
                <li{{if eq .Active "societe"}} class="active"{{end}}><a href="/societe">Soci&eacutet&eacute</a></li>
                <li{{if eq .Active "dossier"}} class="active"{{end}}><a href="/dossier">Dossier</a></li>
                <li{{if eq .Active "temps"}} class="active"{{end}}><a href="/temps">Temps</a></li>
                <li{{if eq .Active "mails"}} class="active"{{end}}><a href="/mails">Mails</a></li>
            </ul>
          </div>
        </nav>
{{end}}
Run Code Online (Sandbox Code Playgroud)

在main.go中:

var FuncMap = template.FuncMap{
    "eq": func(a, b interface{}) bool {
        return a == b
    },
}
var …
Run Code Online (Sandbox Code Playgroud)

if-statement go go-templates

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

vue-resource入门

为了找到vue.js的vue-resource插件的任何预先使用的示例而苦苦挣扎,我尝试了这个:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.7/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/0.1.13/vue-resource.min.js"></script>

<div id="my_view">
  <p>{{ origin }}</p>
</div>

<script>
var Vue = require('vue');
Vue.use(require('vue-resource'));

new Vue({
    el: '#my_view',
    data: {
       origin: ''
    },

    ready: function() {

        // GET request
        this.$http.get('http://httpbin.org/ip', function (data, status, request) {

            // set data on vm
            this.$set('origin', data)

        }).error(function (data, status, request) {
            // handle error
        })
      }
})
</script>
Run Code Online (Sandbox Code Playgroud)

只是查询httpbin.org/ip(我能找到的随机REST端点)并在里面显示结果#myview > p.这只是我正在尝试运行的vue-resource github页面上提供的示例(一个改编版本).

任何人都可以看到我无法实现这一目标吗?

编辑:添加逗号,这里是它的小提琴.

javascript javascript-framework vue.js

6
推荐指数
1
解决办法
2万
查看次数

汇总:从外部模块捆绑/嵌入 wasm 代码

使用汇总,我试图捆绑一个 typescript 库,该库导入和调用包含 wasm 文件的 npm 模块。

只有生成的包不包含 wasm 文件内容的痕迹。我怎样才能强制它也捆绑 webassembly?


这是我尝试过的关键文件:

// typescript/src/index.ts

import * as libEd from "ed25519-raw";

export const seed_from_phrase = libEd.seed_from_phrase;
export const gen_keypair = libEd.gen_keypair;
Run Code Online (Sandbox Code Playgroud)

我的 package.json 是

{
  "name": "ed25519",
  "version": "0.1.0",
  "description": "ed25519",
  "main": "typescript/dist/bundle.cjs.js",
  "types": "typescript/src/index.ts",
  "dependencies": {
    "ed25519-raw": "git+https://github.com/cmanteiga/ed25519-raw"
  },
  "devDependencies": {
    "@types/node": "^13.7.1",
    "typescript": "^3.7.5",
    "@rollup/plugin-commonjs": "^11.0.2",
    "@rollup/plugin-multi-entry": "^3.0.0",
    "@rollup/plugin-node-resolve": "^7.1.1",
    "@rollup/plugin-wasm": "^3.0.0",
    "rollup": "^1.31.1",
    "rollup-plugin-typescript2": "^0.26.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

最后汇总配置是

{
  "name": "ed25519",
  "version": "0.1.0",
  "description": "ed25519",
  "main": "typescript/dist/bundle.cjs.js",
  "types": …
Run Code Online (Sandbox Code Playgroud)

rollup typescript webassembly wasm-bindgen

6
推荐指数
0
解决办法
508
查看次数

从 Swift 调用 Rust

在 Rust 方面,我编写了一个函数,该函数返回一个字符串作为字节指针(在内存中作为 C 结构布局):

#[repr(C)]
pub struct RustByteSlice {
    pub bytes: *const u8,
    pub len: size_t,
}

#[no_mangle]
pub extern "C" fn get_string_from_rust() -> RustByteSlice {
    let s = "This is a string from Rust.";
    RustByteSlice {
        bytes: s.as_ptr(),
        len: s.len() as size_t,
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 cbindgen 为它生成头文件时,它给了我以下输出:

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct {
  const uint8_t *bytes;
  size_t len;
} RustByteSlice;

RustByteSlice get_string_from_rust(void);

char *hello(const char *to);

void hello_release(char *s);

void utf8_bytes_to_rust(const uint8_t *bytes, …
Run Code Online (Sandbox Code Playgroud)

c ffi rust swift

6
推荐指数
1
解决办法
1618
查看次数