我们想使用Go来实现我们的业务逻辑,但是我们找不到Go的规则引擎/推理引擎的任何良好实现。有没有人有任何经验或建议?
基本上我的文件夹结构如下:
bin/
pkg/
linux_amd64/
github.com/user/
stringutil.a
src/
github.com/user/
hello/
hello.go (main file)
stringutil/
stringutil.go
Run Code Online (Sandbox Code Playgroud)
hello.go
是从 导入一些函数的主程序stringutil.go
。
运行后go install
,在(如上)stringutil.a
下创建。pkg
我的问题是,既然我已经有了源,我可以在go install
没有源的情况下再次运行吗?stringutil.go
stringutil.a
我是一名 Java 程序员,也是 Go 新手。通常在Java中,在编译时,我们可以将编译好的jar文件导入到classpath中进行编译。
我怎样才能在 Go 中做同样的事情?如果我有一个编译好的包,stringutil.a
我怎样才能将该文件分发给某人并让他们使用它而不暴露原始源代码?
我已经编写了REST API服务,该服务要求所有响应均为JSON。但是,当Go HTTP请求解析器遇到错误时,它将返回400作为纯文本响应,而无需调用我的处理程序。例:
> curl -i -H 'Authorization: Basic hi
there' 'http://localhost:8080/test' -v
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /test HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Authorization: Basic hi
> there
>
< HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
< Content-Type: text/plain; charset=utf-8
Content-Type: text/plain; charset=utf-8
< Connection: close
Connection: close
<
* Closing connection 0
Run Code Online (Sandbox Code Playgroud)
请注意无效的授权标头。当然,400是正确的响应,但是当然是文本/纯文本。有什么方法可以配置Go http解析器以使用自定义错误响应媒体类型和主体?
例:
type myType struct {
foo []float64
Name string
}
Run Code Online (Sandbox Code Playgroud)
myType
未导出,但导出其中的Name
字段.
这样做有意义吗?这被认为是一种不好的做法吗?
我有这样的东西,编译得很好.Name
如果我创建一个导出的数组,我可以访问该字段myType
var MyArray []myType = {... some initialization }
fmt.Println(MyArray[0].Name) // Name is visible and it compiles
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Go 中发出一个简单的 HTTP 请求,在直接按照指南操作后,我不断收到相同的错误:
local error: tls: no renegotiation
我不太明白如何解释这个?我知道这不是服务器上的问题,因为当我从 python 调用相同的请求时,它返回正常。这是我的代码:
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
func main() {
timeout := time.Duration(20 * time.Second)
client := &http.Client{
Timeout: timeout,
}
data := url.Values{
"top": {"10"},
"lDate": {"2019-01-01"},
}
req, err := http.NewRequest("POST", "https://api.*********.com/AppAvailLoads?", strings.NewReader(data.Encode()))
if err != nil {
fmt.Println("Error in construction")
}
req.Header.Add("x-cdata-authtoken", "********")
req.Header.Add("content-type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error in request")
fmt.Println(err)
} else { …
Run Code Online (Sandbox Code Playgroud) 在Go规范的包初始化部分,"词汇文件名顺序"是什么意思?
为了确保可重现的初始化行为,建议构建系统以词法文件名顺序将属于同一个包的多个文件呈现给编译器.
我正在尝试解决Golang游览练习rot13Reader:
这是我的解决方案:
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func rot13(x byte) byte {
switch {
case x >= 65 && x <= 77:
fallthrough
case x >= 97 && x <= 109:
x = x + 13
case x >= 78 && x <= 90:
fallthrough
case x >= 110 && x >= 122:
x = x - 13
}
return x
}
func (r13 *rot13Reader) Read(b []byte) (int, …
Run Code Online (Sandbox Code Playgroud) 当我尝试编译此代码时:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func main() {
fmt.Println("Hello, playground")
}
const (
GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
)
type Route struct {
Name string `json:"name"`
Method string `json:"method"`
Pattern string `json:"pattern"`
HandlerFunc http.HandlerFunc `json:"-"`
}
type Routes []Route
var routes = Routes{
Route{
Name: "GetRoutes",
Method: GET,
Pattern: "/routes",
HandlerFunc: GetRoutes,
},
}
func GetRoutes(res http.ResponseWriter, req *http.Request) {
if err := json.NewEncoder(res).Encode(routes); err != nil {
panic(err) …
Run Code Online (Sandbox Code Playgroud) 我将struct传递给函数interface{}
。然后在内部使用它reflect
来获取struct属性。这是代码:
func (db *DB) Migrate(domain ...interface{}) {
// statement := "CREATE TABLE IF NOT EXISTS %s (%s, %s, %s, %s, %s)"
for _,i := range domain {
params := BindStruct(&i)
statement := CreateStatement("create", len(params))
_,err := db.Exec(fmt.Sprintf(statement, params...))
if err != nil {
log.Fatal("Error migrating database schema - ", err)
break
}
}
}
func BindStruct(domain interface{}) (params []interface{}) {
tableName := reflect.TypeOf(domain).Elem().Name()
params = append(params, tableName)
val := reflect.ValueOf(domain).Elem()
for i:=0; i < val.NumField(); i++ …
Run Code Online (Sandbox Code Playgroud) 我创建了以下 YAML 文件来提供用户需要提供的一些配置:
Environments:
sys1:
models:
- app-type: app1
service-type: “fds"
- app-type: app2
service-type: “era”
sys2:
models:
- app-type: app1
service-type: “fds"
- app-type: app2
service-type: “era"
Run Code Online (Sandbox Code Playgroud)
https://codebeautify.org/yaml-validator/cbb349ec
我这里有:
sys
sys
包含 1..n 个具有关键 app-type 的模型实例现在我需要解析这个 YAML 文件,所以我尝试构建一个结构类型,如:
type Environment struct {
Environment [] sys
}
type sys struct{
Models []Properties
}
type Models struct{
app-type string `yaml:"app-type"`
service-type string `yaml:"service-type"`
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试解析这个 YAML,我得到一个索引超出范围的错误。
我的问题是:
1. Do I model the YAML correctly?
2. Do …
Run Code Online (Sandbox Code Playgroud) go ×10
bad-request ×1
go-http ×1
http ×1
media-type ×1
reflection ×1
rule-engine ×1
struct ×1
yaml ×1