doujie3888 2017-10-21 11:07
浏览 145

如何像python一样去打印ascii文本

how to print ascii-text in go language like python does like picture shown below

Using python

enter image description here

Using Golang

enter image description here

  • 写回答

2条回答 默认 最新

  • dsf55s1233 2017-10-21 11:30
    关注

    The problem is that your text contains backtick (`), which happen to be delimiter character for golang's raw string literal. This situation is comparable to your python code had your text contains 3 consecutive double-quotes, which is the delimiter being used in your python code.

    I don't see any quick escape from this situation without modifying your ascii text, as we don't have other options for raw string delimiter in golang like we have in python. You may want to store your ascii text in a text file and read it from there :

    import (
        ....
        ....
        "io/ioutil"
    )
    
    func banner() string {
        b, err := ioutil.ReadFile("ascii.txt")
        if err != nil {
            panic(err)
        }
        fmt.Println(string(b))
    }
    

    If you're ok with slight modification to the ascii text source, then you can temporarily use other character that isn't used anywhere else in the ascii text to represent backtick, and then do string replacement to put the actual backtick in place. Or, you can use fmt.Sprintf to supply the problematic backtick :

    ascii := fmt.Sprintf(`....%c88b...`, '`')
    fmt.Println(ascii)
    // output:
    // ....`88b...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)