go

command
v0.0.0-...-b73ddd5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2017 License: Apache-2.0 Imports: 4 Imported by: 0

README

Tutorial 1: Go Function w/ Input (3 minutes)

这个例子将告诉你如何测试和部署 Go 代码到 Fn. 它还会演示如何通过 stdin 传输数据.

首先, 运行下列命令:
# 初始化函数, 并创建 func.yaml 文件
fn init --name hello-go

# 测试函数的效果. 会在一个函数中运行, 就像在服务器中所表现的一样
fn run

# 现在试试用 stdin 输入下数据
cat sample.payload.json | fn run

# 把函数部署到 Fn 服务器上 (默认是 localhost:8080)
# 同时也会创建一个到该函数的路由
fn deploy --app myapp
现在调用上面的函数:
curl http://localhost:8080/r/myapp/hello-go

或者在浏览器打开: http://localhost:8080/r/myapp/go

再试试输入 JSON 数据:

curl -H "Content-Type: application/json" -X POST -d @sample.payload.json http://localhost:8080/r/myapp/hello-go

That's it!

关于依赖

对于 Go 程序, 直接把依赖放到 vendor/ 目录就可以了.

In Review

  1. 我们在命令行通过管道的方式将 JSON 数据传给函数

    cat sample.payload.json | fn run
    
  2. 我们在函数中通过 stdin 接收输入

    json.NewDecoder(os.Stdin).Decode(p)
    
  3. 我们将输出写到 stdout

    fmt.Printf("Hello")
    
  4. 我们将 stderr 发送到系统日志

    log.Println("here")
    

接下来

第二部分: 输入参数

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL