NAV
Go Shell Ruby Python Javascript

Introduction

Welcome to the Kittn API! You can use our API to access Kittn API endpoints, which can get information on various cats, kittens, and breeds in our database.

We have language bindings in Shell, Ruby, and Python! You can view code examples in the dark area to the right, and you can switch the programming language of the examples with the tabs in the top right.

This example API documentation page was created with DocuAPI, a multilingual documentation theme for the static site generator Hugo.

Authentication

To authorize, use this code:

package main

import "github.com/bep/kittn/auth"

func main() {
	api := auth.Authorize("meowmeowmeow")

	// Just to make it compile
	_ = api
}
require 'kittn'

api = Kittn::APIClient.authorize!('meowmeowmeow')
import kittn

api = kittn.authorize('meowmeowmeow')
# With shell, you can just pass the correct header with each request
curl "api_endpoint_here"
  -H "Authorization: meowmeowmeow"
const kittn = require('kittn');

let api = kittn.authorize('meowmeowmeow');

Make sure to replace meowmeowmeow with your API key.

Kittn uses API keys to allow access to the API. You can register a new Kittn API key at our developer portal.

Kittn expects for the API key to be included in all API requests to the server in a header that looks like the following:

Authorization: meowmeowmeow

Kittens

Get All Kittens

package main

import "github.com/bep/kittn/auth"

func main() {
	api := auth.Authorize("meowmeowmeow")

	_ = api.GetKittens()
}
require 'kittn'

api = Kittn::APIClient.authorize!('meowmeowmeow')
api.kittens.get
import kittn

api = kittn.authorize('meowmeowmeow')
api.kittens.get()
curl "http://example.com/api/kittens"
  -H "Authorization: meowmeowmeow"
const kittn = require('kittn');

let api = kittn.authorize('meowmeowmeow');
let kittens = api.kittens.get();

The above command returns JSON structured like this:

[
  {
    "id": 1,
    "name": "Fluffums",
    "breed": "calico",
    "fluffiness": 6,
    "cuteness": 7
  },
  {
    "id": 2,
    "name": "Max",
    "breed": "unknown",
    "fluffiness": 5,
    "cuteness": 10
  }
]

This endpoint retrieves all kittens.

HTTP Request

GET http://example.com/api/kittens

Query Parameters

ParameterDefaultDescription
include_catsfalseIf set to true, the result will also include cats.
availabletrueIf set to false, the result will include kittens that have already been adopted.

Get a Specific Kitten

package main

import "github.com/bep/kittn/auth"

func main() {
	api := auth.Authorize("meowmeowmeow")

	_ = api.GetKitten(2)
}
require 'kittn'

api = Kittn::APIClient.authorize!('meowmeowmeow')
api.kittens.get(2)
import kittn

api = kittn.authorize('meowmeowmeow')
api.kittens.get(2)
curl "http://example.com/api/kittens/2"
  -H "Authorization: meowmeowmeow"
const kittn = require('kittn');

let api = kittn.authorize('meowmeowmeow');
let max = api.kittens.get(2);

The above command returns JSON structured like this:

{
  "id": 2,
  "name": "Max",
  "breed": "unknown",
  "fluffiness": 5,
  "cuteness": 10
}

This endpoint retrieves a specific kitten.

HTTP Request

GET http://example.com/kittens/<ID>

URL Parameters

ParameterDescription
IDThe ID of the kitten to retrieve

Errors

The Kittn API uses the following error codes:

Error CodeMeaning
400Bad Request – Your request sucks
401Unauthorized – Your API key is wrong
403Forbidden – The kitten requested is hidden for administrators only
404Not Found – The specified kitten could not be found
405Method Not Allowed – You tried to access a kitten with an invalid method
406Not Acceptable – You requested a format that isn't json
410Gone – The kitten requested has been removed from our servers
418I'm a teapot
429Too Many Requests – You're requesting too many kittens! Slow down!
500Internal Server Error – We had a problem with our server. Try again later.
503Service Unavailable – We're temporarially offline for maintanance. Please try again later.

Here is problem link download

And First Version is greedy algorithm

多多进宝计算器

多多进宝

尚小德

你的结果 尚小德

成本 + 运费 + 利润 )➗ (100% - 推手佣金-平台佣金) + 优惠券价格

填入你的数字

输入你的成本价格.
输入你的运费.
输入你的利润.
10%

推手佣金百分比需要输入小数,如 10% 则填入 0.1

10%

平台佣金占推手佣金的 10%

20

Refer

Go 语言中的修饰器编程

关于使用 bufio.Scanner 读取数据

[关于字符串拼接如何最快](

https://gocn.vip/question/265)

参考结论 单次调用性能:操作符+>strings.Join>=bytes.Buffer>fmt.Sprintf 灵活性:bytes.Buffer>fmt.Sprintf>=strings.Join>操作符+

[使用 strings.Builder] (https://www.reddit.com/r/golang/comments/7j65d0/new_in_go_110_stringsbuilder_efficiently_build/)

etcd 中使用 grpc 的设计

首先在最外层设计 API

type Authenticator interface {
	AuthEnable(ctx context.Context, r *pb.AuthEnableRequest) (*pb.AuthEnableResponse, error)
	AuthDisable(ctx context.Context, r *pb.AuthDisableRequest) (*pb.AuthDisableResponse, error)
	Authenticate(ctx context.Context, r *pb.AuthenticateRequest) (*pb.AuthenticateResponse, error)
	UserAdd(ctx context.Context, r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error)
	UserDelete(ctx context.Context, r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error)
	UserChangePassword(ctx context.Context, r *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error)
	UserGrantRole(ctx context.Context, r *pb.AuthUserGrantRoleRequest) (*pb.AuthUserGrantRoleResponse, error)
	UserGet(ctx context.Context, r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error)
	UserRevokeRole(ctx context.Context, r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error)
	RoleAdd(ctx context.Context, r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error)
	RoleGrantPermission(ctx context.Context, r *pb.AuthRoleGrantPermissionRequest) (*pb.AuthRoleGrantPermissionResponse, error)
	RoleGet(ctx context.Context, r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error)
	RoleRevokePermission(ctx context.Context, r *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error)
	RoleDelete(ctx context.Context, r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error)
	UserList(ctx context.Context, r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error)
	RoleList(ctx context.Context, r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error)
}

API 设计为 interface ,可以使用 http , grpc 等进行扩展 API 调用

下面是 扩展 grpc 调用。

包装一层 AuthServer 来满足 grpc 的 server 方法定义

使用满足 Authenticator 接口 的 结构 作为调用 函数的入口

非常像 mvc 中 , controller 对 model 的操作。

type AuthServer struct {
	authenticator etcdserver.Authenticator
}

func NewAuthServer(s *etcdserver.EtcdServer) *AuthServer {
	return &AuthServer{authenticator: s}
}

func (as *AuthServer) AuthEnable(ctx context.Context, r *pb.AuthEnableRequest) (*pb.AuthEnableResponse, error) {
	resp, err := as.authenticator.AuthEnable(ctx, r)
	if err != nil {
		return nil, togRPCError(err)
	}
	return resp, nil
}

···

quickstart use fileserver.go

go run fileserver.go

add fileserver as a http handler should be more careful about prefix.

so fileserver

go run filehandler.go

GitHub 上 搜索 fsnotify

FileSystem 事件生成器.

https://gioui.org/

https://github.com/ipfs/go-ipfs/blob/master/docs/examples/go-ipfs-as-a-library/README.md

From

Find Tutorials detail

Usage

编译镜像

docker build -t mongo-replica-insert .

参考的输出如下所示

fengyifeideMacBook-Air:insert fengyfei$ docker build -t mongo-replica-insert .
Sending build context to Docker daemon  3.584kB
Step 1/10 : FROM golang:1.13.1-alpine3.10
 ---> 48260c3da24c
Step 2/10 : WORKDIR /app
 ---> Running in 149be8a6b4dc
Removing intermediate container 149be8a6b4dc
 ---> 8b52d563570a
Step 3/10 : COPY main.go .
 ---> b1dd207d458b
Step 4/10 : ENV GOPROXY "https://goproxy.io"
 ---> Running in 2565bc60b053
Removing intermediate container 2565bc60b053
 ---> 5f4dd7398686
Step 5/10 : RUN env | grep GO
 ---> Running in 7bed5a0f1756
GOPATH=/go
GOPROXY=https://goproxy.io
GOLANG_VERSION=1.13.1
Removing intermediate container 7bed5a0f1756
 ---> 19e431e016ed
Step 6/10 : RUN go mod init 'github.com/silverswords/mongo-insight'
 ---> Running in 51392e80d86f
go: creating new go.mod: module github.com/silverswords/mongo-insight
Removing intermediate container 51392e80d86f
 ---> 32289dfe1ff2
Step 7/10 : RUN go mod tidy
 ---> Running in f77dfbe14b6c
go: finding go.mongodb.org/mongo-driver v1.1.1
go: downloading go.mongodb.org/mongo-driver v1.1.1
go: extracting go.mongodb.org/mongo-driver v1.1.1
go: finding github.com/stretchr/testify v1.4.0
go: downloading github.com/stretchr/testify v1.4.0
go: finding github.com/go-stack/stack v1.8.0
go: finding github.com/golang/snappy v0.0.1
go: finding github.com/google/go-cmp v0.3.1
go: finding github.com/tidwall/pretty v1.0.0
go: finding github.com/xdg/stringprep v1.0.0
go: finding golang.org/x/sync latest
go: finding github.com/xdg/scram latest
go: downloading github.com/golang/snappy v0.0.1
go: downloading github.com/go-stack/stack v1.8.0
go: downloading github.com/google/go-cmp v0.3.1
go: downloading github.com/xdg/stringprep v1.0.0
go: downloading github.com/tidwall/pretty v1.0.0
go: downloading golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
go: downloading github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c
go: extracting github.com/go-stack/stack v1.8.0
go: extracting github.com/stretchr/testify v1.4.0
go: extracting github.com/golang/snappy v0.0.1
go: extracting github.com/google/go-cmp v0.3.1
go: extracting github.com/xdg/stringprep v1.0.0
go: extracting github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c
go: extracting golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
go: extracting github.com/tidwall/pretty v1.0.0
go: downloading gopkg.in/yaml.v2 v2.2.2
go: downloading github.com/davecgh/go-spew v1.1.0
go: downloading github.com/pmezard/go-difflib v1.0.0
go: extracting gopkg.in/yaml.v2 v2.2.2
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: extracting github.com/pmezard/go-difflib v1.0.0
go: extracting github.com/davecgh/go-spew v1.1.0
go: extracting gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: finding golang.org/x/text v0.3.2
go: finding golang.org/x/crypto latest
go: downloading golang.org/x/text v0.3.2
go: downloading golang.org/x/crypto v0.0.0-20191001170739-f9e2070545dc
go: extracting golang.org/x/crypto v0.0.0-20191001170739-f9e2070545dc
go: extracting golang.org/x/text v0.3.2
Removing intermediate container f77dfbe14b6c
 ---> 3888fc235f10
Step 8/10 : RUN go mod download
 ---> Running in 251d2c5c5811
go: finding github.com/davecgh/go-spew v1.1.0
go: finding github.com/pmezard/go-difflib v1.0.0
go: finding github.com/stretchr/objx v0.1.0
go: finding golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
go: finding golang.org/x/sys v0.0.0-20190412213103-97732733099d
go: finding golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e
go: finding gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: finding gopkg.in/yaml.v2 v2.2.2
Removing intermediate container 251d2c5c5811
 ---> a05c624c6a4d
Step 9/10 : RUN go build -o main .
 ---> Running in 94da112e6d68
Removing intermediate container 94da112e6d68
 ---> 051c01aee617
Step 10/10 : CMD ["./main"]
 ---> Running in 32a511a74bb4
Removing intermediate container 32a511a74bb4
 ---> 73a0740d612b
Successfully built 73a0740d612b
Successfully tagged mongo-replica-insert:latest

获取 Docker 网络信息

 fengyifeideMacBook-Air:insert fengyfei$ docker network ls
NETWORK ID          NAME                              DRIVER              SCOPE
890b8fb02d3f        bridge                            bridge              local
3908e9135cf4        host                              host                local
e96c036b173f        mongo-replica_mongo-replica-net   bridge              local
4b3c31fe2cb4        mongo_default                     bridge              local
7c7d635714e1        none                              null                local
06ced6455d15        strapi-docker_default             bridge              local

在 Host 的 Shell 中执行

docker run --network mongo-replica_mongo-replica-net -d mongo-replica-insert

执行完毕后,进入 Mongo Replica 的 Primary 节点,确认

dev:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
simple  0.000GB
dev:PRIMARY> use simple
switched to db simple
dev:PRIMARY> db.article.find()
{ "_id" : ObjectId("5d941e116588cf7cc4af8d3b"), "title" : "T-0", "abstract" : "Abstract 0", "reads" : NumberLong(20) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d3c"), "title" : "T-1", "abstract" : "Abstract 1", "reads" : NumberLong(21) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d3d"), "title" : "T-2", "abstract" : "Abstract 2", "reads" : NumberLong(22) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d3e"), "title" : "T-3", "abstract" : "Abstract 3", "reads" : NumberLong(23) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d3f"), "title" : "T-4", "abstract" : "Abstract 4", "reads" : NumberLong(24) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d40"), "title" : "T-5", "abstract" : "Abstract 5", "reads" : NumberLong(25) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d41"), "title" : "T-6", "abstract" : "Abstract 6", "reads" : NumberLong(26) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d42"), "title" : "T-7", "abstract" : "Abstract 7", "reads" : NumberLong(27) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d43"), "title" : "T-8", "abstract" : "Abstract 8", "reads" : NumberLong(28) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d44"), "title" : "T-9", "abstract" : "Abstract 9", "reads" : NumberLong(29) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d45"), "title" : "T-10", "abstract" : "Abstract 10", "reads" : NumberLong(30) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d46"), "title" : "T-11", "abstract" : "Abstract 11", "reads" : NumberLong(31) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d47"), "title" : "T-12", "abstract" : "Abstract 12", "reads" : NumberLong(32) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d48"), "title" : "T-13", "abstract" : "Abstract 13", "reads" : NumberLong(33) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d49"), "title" : "T-14", "abstract" : "Abstract 14", "reads" : NumberLong(34) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d4a"), "title" : "T-15", "abstract" : "Abstract 15", "reads" : NumberLong(35) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d4b"), "title" : "T-16", "abstract" : "Abstract 16", "reads" : NumberLong(36) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d4c"), "title" : "T-17", "abstract" : "Abstract 17", "reads" : NumberLong(37) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d4d"), "title" : "T-18", "abstract" : "Abstract 18", "reads" : NumberLong(38) }
{ "_id" : ObjectId("5d941e116588cf7cc4af8d4e"), "title" : "T-19", "abstract" : "Abstract 19", "reads" : NumberLong(39) }
Type "it" for more
dev:PRIMARY>

https://golang.org

docker run -itd --name redis-test -p 6379:6379 redis

Run sample

go run pub.go

this means if there is a parameter in func like (…someStruct). You could pass none to this func.

Over

  1. 匿名嵌入结构体的时候的 调用顺序的问题

  2. 使用 嵌入式的结构体满足接口

操作

查看 例子,自己想一下运行结果

运行试试看

将例子中的一些结构体和字段注释后实验更多次

注意

结构体里有多个匿名字段的时候,同名方法无法分辨

扩展

对更多相关知识感兴趣可以放链接在这里

https://knative.dev/docs/serving/samples/hello-world/helloworld-go/index.html

Rust Generic Sample

https://www.yuque.com/abser/telegram/btmgmh#comment-422996

this sample for using unix socket to get communicate.

You could change the client code to python , js , or other languages.

Usage

go run unixsocket.go
go run socketclient.go

input something on second terminal and find the same in first terminal.

https://github.com/linjiajian999/blog/blob/master/web/webassembly/%E4%BD%BF%E7%94%A8go%E7%BC%96%E5%86%99webassembly.md

Go wasm