duan35557593 2016-12-29 18:19
浏览 152

从PHP运行Go脚本

I have a hosting with a PHP file which gets the request, take a string from it and have to give to Go (GoLang) script. How could I do it?

package main My GO script:

package main

import (
"log"
"fmt"
"io/ioutil"
"strings"
ivona "github.com/jpadilla/ivona-go"
)

func main() {
    client := ivona.New("GDNAICTDMLSLU5426OAA", "2qUFTF8ZF9wqy7xoGBY+YXLEu+M2Qqalf/pSrd9m")
    text, err := ioutil.ReadFile("/Users/Igralino/Desktop/text.txt")
    if err != nil {
        log.Fatal(err)
    }

    arrayOfParagraphs := strings.Split(string(text), "

")
    i := 0
    for _,paragraph := range arrayOfParagraphs {
        paragraph = strings.TrimSpace(paragraph)
        if (len(paragraph) < 1) { // against empty lines
            continue
        }
        log.Printf("%v
", paragraph)
        options := ivona.NewSpeechOptions(paragraph)
        options.Voice.Language = "ru-RU"
        options.Voice.Name = "Maxim"
        options.Voice.Gender = "Male"
        options.OutputFormat.Codec = "MP3"
        r, err := client.CreateSpeech(options)
        if err != nil {
            log.Fatal(err)
        }

        i++
        file := fmt.Sprintf("/Users/Igralino/Desktop/tts%04d.MP3", i) // files like 0001.ogg
        ioutil.WriteFile(file, r.Audio, 0644)
    }
}
  • 写回答

1条回答 默认 最新

  • doubu1950 2016-12-29 20:16
    关注

    Go is not a scripting language! It is a compiled one.

    Compiled languages

    A compiled language is a programming language whose implementations are typically compilers (translators that generate machine code from source code), and not interpreters (step-by-step executors of source code, where no pre-runtime translation takes place).

    So usage of "script" term is dramatically wrong!

    How to communicate with Go program from other programs.

    You must build and install your program first. This is easy if you already prepared and configured your Go environment:

    $go install github.com/user/hello
    

    Than you can invoke it from command line since it is installed to OS.

    $hello
    

    If your program should expect arguments use flag package to declare it.

    $hello -name <value>
    

    Obviously you can call it from PHP with system function or whatever is possible (I don't know nothing about PHP).

    Other way is to design your Go program as a daemon and communicate with it directly through a port for example. Your Go program must run continuously and listen for a port. A good answer about daemonization of Go programs.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值