Giter Club home page Giter Club logo

build-web-application-with-golang's Introduction

Hi there 👋. You're probably interested in my open source work, so here is a quick summary.

📫 Please open a GitHub issue if you want to discuss something related to an open source project of mine.

Otherwise you can reach me at [email protected]

Last Update: 2022-04-22

Open Source

visitor badge

build-web-application-with-golang's People

Contributors

astaxie avatar border avatar codingsince1985 avatar deepanchor avatar digitalcraftsman avatar ernix avatar goryudyuma avatar gustavokuklinski avatar hirofumitamori avatar hongruiqi avatar invzhi avatar jameswpm avatar jessonchan avatar jimmy99 avatar kaplanbora avatar kasmanavt avatar mariebehzadi avatar maxupeng avatar mikulely avatar mistadikay avatar neydroid avatar olingcat avatar pasali avatar qinhanlei avatar taveek avatar thewhitetulip avatar vcaesar avatar veincheng avatar wenlei avatar woooha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

build-web-application-with-golang's Issues

本书review:第一章

https://github.com/astaxie/build-web-application-with-golang/blob/master/01.2.md
说明:A是原文或说明是关于哪一小节。B是修改建议

第一章review完毕。最开始一点,在QQ交流过,没有另外给出。

A.文件夹名称代码包名称,当然也允许多级目录
B.不通顺

A.例如在src下面新建了目录$GOPATH/src/github.com/astaxie/beedb 那么这个包名称就是“github.com/astaxie/beedb”
B.这里并非包名称是这个,只是表明包路径

A.注意:package的名称必须和目录名保持一致
B.这里已经讨论过

A.在任意的目录执行如下代码go install mymath
B.这里应该是在当前GOPATH目录的任意目录,比如不能在你设置的GOPATH路径之外
另外这里有一个前提最好强调一下就是需要设置GOPATH,不设置GOPATH结果是不一样的

A.这个.a文件是应用包,相当于一个函数库一样,那么我们如何进行调用呢?
B.矛盾的话也提一下,相当于xxx一样,一样不要吧

A.如何安装该应用,进入该目录执行go install
B.对于设置了GOPATH,不一定非得进入该目录才能执行go install,对于实际项目,更多的是在GOPATH目录(任何一子目录)执行go install mythapp这种形式

A.获取远程包
B.这里是不是最好说一下加上-u参数表示会更新远程包;同时说一下,go get会自动将远程包的依赖也搞定

A.如果是main包,当你执行go build之后,它就会在当前目录下生成一个可执行文件。如果你需要在$GOPATH/bin下生成相应的文件,同样需要执行go install。
B.实际上,go build 加上-o参数可以指定输出到$GOPATH/bin目录

A.我们可以指定go build -o astaxie.exe,默认情况是你的package名,就是你的文件夹名称。
B.这里go build的文档不是这么说的。
The -o flag specifies the output file name. If not specified, the
name is packagename.a (for a non-main package) or the base
name of the first source file (for a main package).

A.(注:实际上,package名在Go语言规范中指代码中“package”后使用的名称,此名称可以与文件夹名不同。默认生成的可执行文件名是文件夹名。)
B.又是包名,又是可执行文件名,普通包和main包最好分开说

A. go fmt
B. go fmt 的,更多时候是用gofmt,而且,需要参数-w,否则格式化结果不会会写入文件。gofmt -w src,可以格式化整个项目

A. 这个命令在内部实际上分成了两步操作:第一步是go build,第二步会把编译好的东西move到$GOPATH/pkg或者$GOPATH/bin。
B. 不能这么说,go build会将结果丢弃(main包除外)。都丢弃了,怎么move?应该是go build中前一部分功能是生成结果,然后丢弃;而go install是生成结果,move到对应目录

A. 关于go工具
B. 所有的工具,建议让读者go help xxx看一下文档

A. 关于开发工具的
B. 没仔细看。不过是否应该加上IntelliJ IDEA,据说现在对Go支持挺好

01.1.md 关于GOROOT的设置是不对的

export GOROOT=$HOME/go的说明是错误的

请参考 http://golang.org/doc/install

The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install them in a different location. If you do this, you will need to set the GOROOT environment variable to that directory when using the Go tools.

For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:

export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin

epub生成问题

生成epub脚本里面 获取全部html的命令
list="README.html ls [1-9]*.html |sort -h LICENSE.html"

19章是0109.html
所以要改成上面的命令 ls [0-9]*.html

1.1里64位系统显示

<一段描述> x86\_64 x86\_64 x86\_64 GNU/Linux

\是不是可以删掉?
另外,使用uname -a,Arch和Ubuntu 12.04都是只输出一个x86_64

<一段描述> x86_64 GNU/Linux

1.4 Go开发工具 遇到的问题。

我在Mac ML上,根据1.4 Go开发工具这一节,配置gocode, auto complete, emacs 发现不起作用。只有通过终端运行emacs 才起作用, e.g: open -a emacs
各位同学有没有遇到这种情况?

2.3 章中有描述不准确

函数小节的多个返回值部分这样写:

但如果你的函数是导出的(首字母大写),官方建议,不要命名返回值,因为这样会造成生成的文档不易读。

可是 Effective Go 中 Named result parameters 这一节是这样说的:

The names are not mandatory but they can make code shorter and clearer: they're documentation.

大概意思是:
命名返回值不是强制性的, 但这样做能使代码更短, 更简洁; 读起来像文档一样.

两者意思正好相反呀;

1.2 编译应用 切换目录的问题

cd $GOPATH/pkg/$GOOS_$GOARCH/mymath.a

首先,这是个文件,用cd命令是不行的;
其次,由于_的缘故,要写成${GOOS}_${GOARCH},否则切换到的是$GOPATH/pkg/$GOARCH目录。这个是否跟linux系统有关系?

申明和声明

根据《现代汉语词典》的解释,“申明”是“郑重说明”的意思,“声明”是“公开表示态度或说明真相”,用做名词时,指“声明的文告”。考察两者的不同之处,“声明”重在公开宣布,以让公众知道;“申明”重在说明,以说服对方。

http://wenda.tianya.cn/question/33071267b2022fa7

= =

ch 2.7: var name same as func name

In Chapter 02.7.md:

func sum(a []int, c chan int) {
    sum := 0
    for _, v := range a {
        sum += v
    }
    c <- sum  // send sum to c
}

Should not use "sum" as variable in func sum. It's confusing.

goSublime已集成gocode.exe和Margo.exe了。

r12.11.15-1

* MarGo (margo0) and gocode are now bundled with GoSublime and should be in active use.
    Feel free to remove any old source from $GOPATH*/github.com/{nsf/gocode,DisposaBoy/MarGo}
    if you have no use for them in additiion to their respective binaries

建议修改下文件名结构

一个是无序,前面加几个0吧
第二个是,虽然有个目录,但是并不容易快速浏览各个文件是讲什么的,不知道每个文件都是讲什么的,文件名索引后面可以加个章节名称 如 0010.2 本地化资源 .md

3.1节对URL的描述不一致

URL和DNS解析那里
schema://host[:port#]/path/.../[?query-string][#anchor]
scheme 指定低层使用的协议(例如:http, https, ftp)
上面用的是schema,下面具体的解释的时候用的是scheme。

第三章 review

A. 通过http包可以很方便的就搭建起来一个可以运行的web服务。
B. 可以运行的web服务?可以运行是否恰当?不要更好吧

A. 然后执行web.exe,这个时候其实已经在9090端口监听tcp链接请求了
B. 改成“http链接”是否更好?

A. 是Go实现Web服务的工作模式的流程图
B. 图上的箭头能否表上1、2之类的序号?

A. Go是通过一个函数来操作这个事情的ListenAndServe来监听起来的
B. 不通顺

A. 首先通过Listener接收请求,其次创建一个Conn
B. 这块完全用文字描述,感觉不太好,是否将Go源码,去掉错误处理之类的,放出来,这样比描述更好。文字加源码(简版源码)。不过后面有一个图,大概类似,你看看代码是否有必要吧。至少提示读者可以跟着Go源码看一下,这样理解的会更透彻。

A. 这个类型默认就实现了ServeHTTP这个接口,即我们调用了HandlerFunc(f),类似强制类型转换f成为HandlerFunc类型,这样f就拥有了ServHTTP方法。
B. 并非类似强制类型转换,实际上在http.HandleFunc("/", sayhelloName) //设置访问的路由的时候,内部已经强制类型转换了。mux.Handle(pattern, HandlerFunc(handler)),也就是注册handler的时候进行了强制类型转换

A. 关于自定义路由的问题
B. 除了书上说的这种,不自己实现Handler接口也是可以做到的,你看是否需要补充

5.5 好像有文字错误

例子1,根据条件id>3,获取20位置开始的10条数据的数据

var allusers []Userinfo
err := orm.Where("id > ?", "3").Limit(10,20).FindAll(&allusers)

那个应该是10位置开始的10条数据

1.1节安装部分的介绍可以优化下

1.安装时源码安装不应该和上面的三个平台安装的部分并列为一个级别
2.最好把通过包管理方式安装的方法也提下,比如mac下用homebrew

其实官网这部分东西写的不错,可以参考下的。

02.6 注释里的重载应该是重写

 // Employee重载Human的Sayhi方法
func (e *Employee) SayHi() {
    fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,
        e.company, e.phone) //Yes you can split into 2 lines here.
}

注释里面的重载应该是重写

有个地方文字不通顺

1.2节,gopath设置,在类似Unux环境***下面:
“为了方便,应该把新建以上文件夹,并且把以上……”
“应该把新建以上文件夹”这里语句不通顺啊!

一些小建议

学习过程中遇到一些不太容易懂的地方,想提个小建议。

4.4 防止多次递交表单:
“上面的代码输出到页面的源码如下:”这句话后面的那个图片出现得很突然,之前都没说checkbox,足球,篮球什么的,忽然就有了这些东西,一下反应不过来

7.4 模板处理
“输出嵌套字段内容”
这里讲到了{{with …}}…{{end}}和{{range …}}{{end}},最好能讲解一下with的用法,刚看的时候以为这个可以直接换成range的写法,不过一下没试成功,应该还是有区别的吧

2.2节 array、slice、map关于描述的疑惑

// 如果内部的元素和外部的一样,那么上面的声明可以简化,直接忽略内部的类型
easyArray := [2][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}}·

这上面说内部元素和外部一样,难道还有内部元素和外部不一样的么?

1.4.md 84行关于vim的设置

2.86行后面的/home/border/gocode/pkg/linux_amd64,没有标注含义
3.emacs设置也存在这个问题 gocode set lib-path 和gocode set 说明不清楚
4.gocode 原文为 “gocode only searches $GOPATH/pkg/$GOOS_$GOARCH and $GOROOT/pkg/$GOOS_$GOARCH

这一段还是请原作者改吧。

在 PATH 中加入 GOPATH/bin

1.2节中 关于 GOPATH/bin 目录,只提到「bin 编译后生成的可执行文件(为了方便,可以把此目录加入到 $PATH 变量中)」

按照 go-wiki中的说明,在 OS X 和 Linux 中把 GOPATH/bin 加入 PATH 的方法是

adding the following expression to PATH will add all $GOPATH/bin directories. {GOPATH//://bin:}/bin

Windows 平台下我也不知道有什么更简洁的方法 但是希望书中能把 wiki 中的这个方法介绍一下。而非之前简单的描述

02.5.md中的method的一个例子中代码在输入输出上存在错误

“你可以在任何的自定义类型中定义任意多的method,接下来让我们看一个复杂一点的例子,”这句下的例子输出为

We have 6 boxes in our set
The volume of the last one is%!(EXTRA float64=64, string=cm³)The color of the last one is%!(EXTRA string=YELLOW)The biggest one is%!(EXTRA string=YELLOW)Let's paint them all black
The color of the second one is YELLOW
Obviously, now, the biggest one is YELLOW

正确的输出应为

We have 6 boxes in our set
The volume of the last one is 64.000000 cm³. The color of the last one is YELLOW. The biggest one is YELLOW. Let's paint them all black
The color of the second one is BLACK
Obviously, now, the biggest one is BLACK

4.1表单处理

我用4.1中的代码处理表单,如果访问的是http://localhost:9090就是正常了,但是访问http://localhost:9090/login这个就有问题了,后台打印日志如下:
2013/03/13 01:03:02 http: panic serving [::1]:49860: runtime error: invalid memory address or nil pointer dereference
/usr/local/go/src/pkg/net/http/server.go:589 (0x4bfd1)
_func_004: buf.Write(debug.Stack())
/usr/local/go/src/pkg/runtime/proc.c:1443 (0xf73e)
panic: reflect·call(d->fn, d->args, d->siz);
/usr/local/go/src/pkg/runtime/runtime.c:128 (0x100f5)
panicstring: runtime·panic(err);
/usr/local/go/src/pkg/runtime/thread_darwin.c:410 (0x12ece)
sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
/usr/local/go/src/pkg/html/template/template.go:51 (0x306a7)
(_Template).Execute: t.nameSpace.mu.Lock()
/Users/luosone/GoYard/gostudy/sourcecode/webForm/src/web/web.go:29 (0x25ea)
login: t.Execute(w, nil)
/usr/local/go/src/pkg/net/http/server.go:703 (0x40e42)
HandlerFunc.ServeHTTP: f(w, r)
/usr/local/go/src/pkg/net/http/server.go:941 (0x41a22)
(_ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r)
/usr/local/go/src/pkg/net/http/server.go:669 (0x40c94)
(*conn).serve: handler.ServeHTTP(w, w.req)
/usr/local/go/src/pkg/runtime/proc.c:271 (0xdc30)
goexit: runtime·goexit(void)

关于自定义error作为函数返回值的描述有些问题

谢老师,这里的描述有些问题。
地址:https://github.com/astaxie/build-web-application-with-golang/blob/master/11.1.md


下面这个代码包括注释来自原文中的例子

func Decode() *SyntaxError { // 错误,将可能导致上层调用者err!=nil的判断永远为true。
    var err *SyntaxError     // 预声明错误变量
    if 出错条件 {
        err = &SyntaxError{}
    }
    return err               // 错误,虽然err变量等于nil,但仍可能导致上层调用者err!=nil的判断为true
}

经过测试,发现上面的代码不会导致·上层调用者err!=nil的判断永远为true·的问题。
而如果函数签名的返回值改成error,才会出现这个问题。

func Decode() error {  // 会导致上层调用者err!=nil的判断永远为true。
    var err *SyntaxError 
    if 出错条件 {
        err = &SyntaxError{}
    }
    return err  
}

官方的推荐的方法是:

func Decode() error {  //这里是error,不是自定义type
    var err *SyntaxError 
    if 出错条件 {
        err = &SyntaxError{}
    }
    return nil               // 这里返回nil
}

参考了:http://golang.org/doc/faq#nil_error

生成epub问题

执行脚本生成的epub只有README页,目录也是这面的,能增加生成mobi的脚本更好

2.5 节发现了一处笔误

是在 "指针作为receivers" 这一部分:

那么如果我们是传Box的指针,那么我们接受的其实是Box的一个copy

应该改为

那么如果我们**_不**_传Box的指针,那么我们接受的其实是Box的一个copy

关于slice的一个错误

原文:aSlice = array[5:] // 等价于aSlice = array[5:9] aSlice包含元素: f,g,h,i,j

--这里包含的元素应该是f,g,h,i吧,已实测

6.1 有段话很费解

session,简而言之就是在服务器上保存用户操作的历史信息。但该方式下,仍然需要将发送请求的客户端与session对象进行对应,所以可以借助cookie机制来获取客户端的标识(即session id),也可以通过GET方式将id提交给服务器。session id,即服务器上session对象文件的名称,由服务器负责产生,保证随机性与唯一性,相当于一个随机密钥,避免在握手或传输中暴露用户真实密码。

可以借助cookie机制来获取客户端的标识(即session id),这里提到可以通过客户端来获取session id,可是后面 session id,即服务器上session对象文件的名称,由服务器负责产生 又说session id是由服务器产生的,前后矛盾,看不懂。。。

issues 7.4

Hi, Astaxie,

I use Go-1.1.1 test below, there is no error or panic, it just omit the unexport email field.

package main

import (
    "bytes"
    "fmt"
    "html/template"
    //"os"
)

type Person struct {
    UserName string
    email string
}

func main() {
    t := template.New("fieldname example")

    //t, _ = t.Parse("hello {{.UserName}}!")
    t, err := t.Parse("hello {{.UserName}}, {{.email}}. ")
    if err != nil {
        fmt.Println(err)
    }

    buf := &bytes.Buffer{}
    p := Person{UserName: "Astaxie", email:"[email protected]"}

    //t.Execute(os.Stdout, p)
    t.Execute(buf, p)

    fmt.Println(buf.String())
}

if I change the {{.email}} to {{.email} panic appeared.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.