package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, PHPers")
}
go run helloworld.go
go build
go fmt helloworld.go
i := 5;
s := "6"
fmt.Printf("i + s: %v", i+s)
// Error
$i = 5;
$s = "6"
echo "i + s: ", $i+$si;
// 11
func addSome(a, b int) {
fmt.Println(a+b)
}
function addSome(int $a, int $b) {
echo $a + $b, "\n";
}
addSome(5, "6 million");
addSome(5, "6 million");
addSome(5, "6 million");
addSome(5, "6 million");
type Article struct {
Id int
Title string
Published time.Time
Body string
Comments []Comment
}
type Article struct {
Id int
Title string
Published time.Time
Body string
Comments []Comment
}
article := Article{
5,
"Go for PHP programmers",
time.Now(),
"...",
}
func (a *Article) Publish() {
a.Published = time.Now()
}
func (a *Article) Publish() {
a.Published = time.Now()
}
func NewArticle() : Article {
// Must return an Article
}
type Publishable interface {
func Publish()
}
type Publishable interface {
func Publish()
}
func PublishThis(p Publishable) {
p.Publish();
}
class Article {
public $id;
public $title;
public $published;
public $body;
function publish() {}
}
interface Publishable {
function publish();
}
$article = new Article();
echo $article instanceof Publishable; // true!
// Adding this method satisfies the fmt.Stringer interface.
func (a Article) String() : string {
return a.Title;
}
fmt.Printf("<h1>%v</h1>", someArticle);
type (
Reader interface {
Read(p []byte) (n int, err error)
}
Writer interface {
Write(p []byte) (n int, err error)
}
ReadWriter interface {
Reader
Writer
}
}
for i := 0; i < 10; i++ {
}
for {
if condition {
break;
}
}
for index, article := range articles {
}
func hi() {
for i := 0; i < 3; i++ {
fmt.Printf("Number %v, hahaha\n")
}
}
hi()
func hi() {
for i := 0; i < 3; i++ {
fmt.Printf("Number %v, hahaha\n")
}
}
go hi()
var hits int
func addHit() {
hits = hits + 1
}
func main() {
hits := 0
addHit()
}
var hits int
func addHit() {
hits = hits + 1
}
func main() {
hits := 0;
go addHit()
}
var hits int
var mutex *sync.Mutex
func addHit() {
mutex.Lock()
hits = hits + 1
mutex.Unlock()
}
func main() {
hits := 0;
mutex = &sync.Mutex{}
go addHit()
}
var hits int64
func addHit() {
atomic.AddInt64(hits, 1)
}
func main() {
hits := 0
go addHit()
}
struct Hit {
UserAgent string
Address string
}
hitChannel := make(chan *Hit)
hits := 0;
go func() {
hit <- hitChannel
hits = hits + 1;
}()
for i:=0; i < 100; i++ {
go func() {
hit := &Hit{"",""}
hitsChannel <- hit
}()
}
for {
select {
case value1 <- channel1:
// do something with value 1
case value2 <- channel2:
// do something with value 2
}
}
curl_multi_select
socket_select
stream_select
mysqli_poll
dat, err := ioutil.ReadFile("file.txt")
if err != nil {
// Handle the error
}
dat, _ := ioutil.ReadFile("file.txt")
dat, err := ioutil.ReadFile("file.txt")
if err != nil {
panic(err)
}
func ParseTextFile() (string, error) {
dat, err := ioutil.ReadFile("file.txt")
if err != nil {
return nil, err
}
// Do something with file
return result, nil
}