RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-543736

David Meyster's questions

Martin Hope
David Meyster
Asked: 2024-11-16 06:30:08 +0000 UTC

戈朗。为 API 请求设置服务器

  • 5

告诉我,是否可以创建一个自动插入标准标头的函数,例如,如果我在 /api 处处理

http.HandleFunc("/api/count_users", countUsersHandler) 
http.HandleFunc("/api/count_payments", countPaymentsHandler) 
http.HandleFunc("/api/average_payments", averagePaymentHandler)

那么在所有情况下,您都需要在每个函数中编写标题:

w.Header().Set("Access-Control-Allow-Origin", "*") 
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/json")

并且在所有情况下您都需要转换为 json:

json.NewEncoder(w).Encode(response)

是否可以创建一个函数(例如 API),并简单地将所有这些自动添加到对 API 的每个请求中?像这样:

func countPaymentsHandler(w http.ResponseWriter, r *http.Request) { 
    var count int 
    err := db.QueryRow("SELECT COUNT(*) FROM payments WHERE `status` = '1'").Scan(&count) 
    if err != nil { 
        http.Error(w, "Ошибка при выполнении запроса", http.StatusInternalServerError) 
        return 
    } 

    response := map[string]int{"count": count} 
    return response 
} 

func API(pattern string, handler func()) { 
    http.HandleFunc("/api/"+pattern, func(w http.ResponseWriter, r *http.Request) { 
        w.Header().Set("Access-Control-Allow-Origin", "*") 
        w.Header().Set("Access-Control-Allow-Headers", "Content-Type") 

        response := handler 
        w.Header().Set("Content-Type", "application/json") 
        json.NewEncoder(w).Encode(response) 
    }) 
} 

func main() { 
    initDB() 
    defer db.Close() 

    API("count_payments", countPaymentsHandler) 

    fmt.Println("Сервер запущен на http://localhost:8080") 
    log.Fatal(http.ListenAndServe(":8080", nil)) 
}
golang
  • 1 个回答
  • 20 Views
Martin Hope
David Meyster
Asked: 2024-04-22 23:24:58 +0000 UTC

如何删除 SQL `upgrades` 表中 `first_item_id` 不唯一的所有元素?

  • 5

任务非常简单,但解决方案并不明确,我多次询问神经网络,每个请求都有不同数量的受影响行

sql
  • 1 个回答
  • 17 Views
Martin Hope
David Meyster
Asked: 2024-04-17 23:46:34 +0000 UTC

如何禁用自动完成功能的内置背景色?

  • 5

我用谷歌搜索,发现了一些显而易见的东西,一如既往的拐杖:

input:-webkit-autofill {
  transition: all 5000s ease-in-out 0s;
}

或者

-webkit-box-shadow: 0 0 0px 1000px (цвет который будет перекрывать) inset !important;

伪解决方案:

autocomplete="off"

嗯,我自己尝试了一下:

.modal .input-container input:autofill,
.modal .input-container input:-webkit-autofill,
.modal .input-container input:-webkit-autofill-strong-password,
.modal .input-container input:-webkit-autofill-strong-password-viewable,
.modal .input-container input:-webkit-autofill-and-obscured {
  background-color: transparent !important;
}

没有帮助。所有测试均在 Safari 中进行

图像

html
  • 1 个回答
  • 17 Views
Martin Hope
David Meyster
Asked: 2023-12-13 21:58:06 +0000 UTC

是不是不再需要ready函数了?

  • 5

从3.0版本开始,如文档中所示jQuery,该功能ready不再需要使用,我想问的是,真的可以用它代替ready吗$()?

例子:

$(document).ready(function () { 
  console.log('DOM загружен с помощью ready!'); 
});

$(function () {
  console.log('DOM загружен с помощью $()!');
});

是否也可以使用该方法直接调用函数?

例子:

function hi(text) {  
  console.log(text);  
}  
$(hi('123'));
javascript
  • 1 个回答
  • 31 Views
Martin Hope
David Meyster
Asked: 2023-12-10 23:15:03 +0000 UTC

为什么输入类型=“范围”(滑块)没有样式?

  • 6

由于 ,此示例中的滑块应为红色background: red;,但它仍然是蓝色:在此输入图像描述

.slider {  
  border: 0;  
  outline: 0;  
  width: 100px;  
  background: red;  
}  

.slider::-webkit-slider-thumb { 
  -webkit-appearance: none; 
  appearance: none; 
  width: 20px; 
  height: 20px; 
  border-radius: 50%; 
  background: red; 
  cursor: pointer; 
} 

.slider::-moz-range-thumb { 
  width: 20px; 
  height: 20px; 
  border-radius: 50%; 
  background: red; 
  cursor: pointer; 
} 

.slider::-ms-thumb { 
  width: 20px; 
  height: 20px; 
  border-radius: 50%; 
  background: red; 
  cursor: pointer; 
} 
<input class="slider" type="range" min="0" max="100">

html
  • 1 个回答
  • 31 Views
Martin Hope
David Meyster
Asked: 2023-05-14 02:06:07 +0000 UTC

制定丢弃对象的算法

  • 5

您需要创建一个用于丢弃对象的算法。

输入数据:

open_cost- 开业成本。

items- 一系列可以掉落的物品。

item['cost']- 项目的成本单独。

您还可以使用:

items_count- 所有项目的数量。

items_cost- 所有项目的成本。

等等。

该算法应该平均给出一个成本等于发现成本的项目。


我需要一个 php 算法。你可以用任何方便的语言来写,对我来说最主要的是理解原理。

我也有自己的算法,但是它的问题是不同的item需要不同的系数,用selection来找。

这是算法:

function getClosest($search, $array)
{
  $num = null;
  foreach ($array as $item) {
    if ($item['chance'] < $search) {
      continue;
    }
    if ($num === null || $item['chance'] < $num['chance']) {
      $num = $item;
    }
  }
  return $num;
}

function getRandomSkin($items, $open_cost, $coeff)
{

  $accum = 0;
  foreach ($items as &$item) {
    if ($open_cost >= $item['cost']) {
      $chance = $item['cost'] / $open_cost * $coeff;
    } else {
      $chance = $open_cost / $item['cost'];
    }

    $item['chance'] = round($chance, 3) * 1000;
    $accum = $skin['chance'] += $accum;
  }

  $rand_num = rand(0, end($items)['chance']);

  $rand_element = getClosest($rand_num, $items);
  return $rand_element;
}

例子:

$items = [
    ['id' => 1, 'cost' => 50],
    ['id' => 2, 'cost' => 100],
    ['id' => 3, 'cost' => 10],
    ['id' => 4, 'cost' => 200],
];

$open_cost = 50;

根据这些数据,掉落物品的几率应该如下:

Формула - $item['cost'] / $open_cost

id_1 - 1
id_2 - 2
id_3 - 5
id_4 - 4

数字越低,掉落的几率就越大。

php
  • 2 个回答
  • 57 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5