丙午🐎年

acc8226 的博客

Go 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 定义接口 */
type interface_name interface {
method_name1 [return_type]
method_name2 [return_type]
method_name3 [return_type]
...
method_namen [return_type]
}

/* 定义结构体 */
type struct_name struct {
/* variables */
}

/* 实现接口方法 */
func (struct_name_variable struct_name) method_name1() [return_type] {
/* 方法实现 */
}
...
func (struct_name_variable struct_name) method_namen() [return_type] {
/* 方法实现*/
}
阅读全文 »

Go 语言通过内置的错误接口提供了非常简单的错误处理机制。

error 类型是一个接口类型,这是它的定义:

1
2
3
type error interface {
Error() string
}

我们可以在编码中通过实现 error 接口类型来生成错误信息。

函数通常在最后的返回值中返回错误信息。使用errors.New 可返回一个错误信息:

1
2
3
4
5
6
func Sqrt(f float64) (float64, error) {
if f < 0 {
return 0, errors.New("math: square root of negative number")
}
// 实现
}
阅读全文 »

Go 语言支持并发,我们只需要通过 go 关键字来开启 goroutine 即可。

goroutine 是轻量级线程,goroutine 的调度是由 Golang 运行时进行管理的。

goroutine 语法格式:

1
go 函数名( 参数列表 )
1
go f(x, y, z)

Go 允许使用 go 语句开启一个新的运行期线程, 即 goroutine,以一个不同的、新创建的 goroutine 来执行一个函数。 同一个程序中的所有 goroutine 共享同一个地址空间。

阅读全文 »

Apache 是世界使用排名第一的 Web 服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的 Web 服务器端软件之一。

1. 执行如下命令,安装 Apache 服务及其扩展包。

1
yum -y install httpd httpd-manual mod_ssl mod_perl mod_auth_mysql

2. 执行如下命令,启动 Apache 服务。

1
systemctl start httpd.service

3. 测试 Apache 服务是否安装并启动成功。

Apache 默认监听 80 端口,所以只需在浏览器访问即可。

阅读全文 »

Jetty 提供了一个 web 服务器和 servlet 容器,另外还提供了对 HTTP/2、 WebSocket、 OSGi、 JMX、 JNDI、 JAAS 和许多其他集成的支持。这些组件是开放源码的,可以免费用于商业用途和分发。

在开发和生产中,Jetty 被广泛应用于各种项目和产品中。Jetty 长期以来深受开发人员的喜爱,因为它可以轻松地嵌入到设备、工具、框架、应用服务器和现代云服务中。

下载地址 https://www.eclipse.org/jetty/download.html

参考文档 Jetty : The Definitive Reference (eclipse.org)

启动和关闭服务脚本

1
2
service jetty start
service jetty stop
阅读全文 »
0%