从 JavaScript 切换到 Golang

2025-05-25

从 JavaScript 切换到 Golang

图像
图片来源

由于我有 JavaScript 背景,所以一直想学习一门静态类型编程语言。今年早些时候,在阅读了 Golang 的相关评论后,我决定学习它。Golang 是由 Google 支持的。当然,Docker、Kubernetes、Terraform 等流行的 DevOps 工具都是用 Golang 构建的。在本文中,我将带您了解 Golang 和 JavaScript 的基本编程。

变量

JavaScript

在 Javascript 中,可以使用let、const(ES6)var(ES5)关键字声明变量。

  // using the const keyword
  const a = 10
  // using the let keyword
  let b = 10
  // using the var keyword
  var c = 10
  console.log(a, b, c) // returns 10, 10, 10
Enter fullscreen mode Exit fullscreen mode

JavaScript 变量游乐场

Golang

在 Go 中,可以使用varconst关键字以及短变量声明语法来声明变量。

  // using the var keyword
  var a = 10 // go detects the type here even though we don't specify
  fmt.Println(a) // returns 10
  fmt.Printf("variable a is of type: %T\n", a) // returns int

  // using the const keyword
  const b = 20  // It is important to note that the value of b must be known at compile-time
  fmt.Println(b) // returns 20

  // variable decalred but not assgined a value returns the zero value of the type
  var c bool
  fmt.Println(c) // returns the zero value(zero value of a boolean is false)

  // using the short variable declaration syntax
  d := "this is a variable" // go detects the type of this variable
  fmt.Println(d) // returns this is a variable
  fmt.Printf("d is of type: %T\n", d) // returns the type(string)
Enter fullscreen mode Exit fullscreen mode

Go 变量游乐场

数组

数组是项目的集合。

JavaScript

在 Javascript 中,数组是动态的,可以从数组中添加和删除项目,而且 Javascript 是一种松散类型的语言,它可以在数组中保存不同类型的值。

  let myArray = [1, "this is array", true, 100.30]
  console.log(myArray) // returns [1, "this is array", true, 100.30]

// we can remove the last item in an array using the pop method
  myArray.pop()
  console.log(myArray) // returns [1, "this is array", true]

// we can add to the end of the array using the push method
  myArray.push(20)
  console.log(myArray) // returns [1, "this is array", true, 20]

// we can remove the first item of the array using the shift method
  myArray.shift()
  console.log(myArray) // returns ["this is array", true, 20]

// we can add to the start of the array using the unshift method
  myArray.unshift(210)
  console.log(myArray) // returns [210, "this is array", true, 20]
Enter fullscreen mode Exit fullscreen mode

JavaScript 数组游乐场

Golang

Go 中的数组长度是固定的,您不能添加或删除数组,而且数组只能包含指定的类型。

    a := [5]string{"a", "b", "c", "d", "e"} // length is 5
    fmt.Println(a) // returns [a b c d e]
    // But what happens if we don't specify exactly 5 items
    b := [5]string{"a", "b", "c"}
    fmt.Printf("%#v", b) // returns [5]string{"a", "b", "c", "", ""}
    // "" represents the zero value(zero value of a string is "")
Enter fullscreen mode Exit fullscreen mode

Go Array Playground
在 Golang 中我们也有切片,它们是动态的,我们不需要指定长度,可以从切片中添加和删除值。

    a := []string{"a", "b", "c"}
    fmt.Printf("%#v", a) //  returns []string{"a", "b", "c"}

    // adding to a slice, we can use the append method to add an item to a slice
    a = append(a, "d")   // append takes in the the array and the value we are adding
    fmt.Printf("%#v", a) // returns []string{"a", "b", "c", "d"}

    // removing from a slice by slicing
    a = append(a[0:3])   // 0 represents the index, while 3 represents the position
    fmt.Printf("%#v", a) // returns []string{"a", "b", "c"}

    // slices can also be created using the make method(in-built)
    // the first value is the type, the second and the third value is the length and maximum capacity of the slice
    b := make([]string, 3, 5)
    fmt.Printf("length of b is:%#v, and cap of b is:%#v\n", len(b), cap(b)) // returns length of b is:3, and cap of b is:5
Enter fullscreen mode Exit fullscreen mode

切片游乐场

功能

JavaScript

在 Javascript 中可以使用function关键字编写函数表达式也可以使用箭头函数(ES6) 。

// using the function keyword
   function a(value) {
       return value
   }
   const val = a("this is the value")
   console.log(val)
// using arrow function
   const b = ((value) => value) 
   const val2 = b("this is another value")
   console.log(val2)
Enter fullscreen mode Exit fullscreen mode

Javascript 函数游乐场

Golang

使用func关键字,可以在 go 中编写函数表达式。

  func a() {
   fmt.Println("this is a function")
}
  a() // returns "this is a function"
// parameters and return type can also be specified
  func b(a,b int) int { // takes in value of type int and returns an int
     result := a * b
   return result
}
  val := b(5,6)
  fmt.Println(val) // returns 30
Enter fullscreen mode Exit fullscreen mode

Go 函数游乐场

对象

JavaScript

在 JavaScript 中,我们可以通过在用逗号分隔的花括号中指定键和值来编写对象。

  const music = {
   genre: "fuji",
   title: "consolidation",
   artist: "kwam 1",
   release: 2010,
   hit: true
}
console.log(music) // returns {genre: "fuji", title: "consolidation", artist: "kwam 1", release: 2010, hit: true}
Enter fullscreen mode Exit fullscreen mode

Javascript 对象游乐场

Golang

在 Golang 中,有一个结构体,它包含一个字段和字段类型

  type Music struct {
    genre   string
    title   string
    artist  string
    release int
    hit     bool
}
ms := Music{
    genre:   "hiphop",
    title:   "soapy",
    artist:  "naira marley",
    release: 2019,
    hit:     true,
}
fmt.Printf("%#v\n", ms) // returns main.Music{genre:"hiphop", title:"soapy", artist:"naira marley", release:2019, hit:true}

Enter fullscreen mode Exit fullscreen mode

Go 结构体游乐场

有用的 Golang 资源

Go 之旅
完成 Go 训练营
RunGo
gobyexample

文章来源:https://dev.to/bjhaid_93/switching-from-javascript-to-golang-15km
PREV
当你害怕公开演讲时,如何在会议上发言
NEXT
您会为下一个 Web 项目选择哪种技术?