https://projecteuler.net/problem=4
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
906609
package main
import "fmt"
func bIsPalindromic(num int) bool {
tNum := num
rNum := 0
for tNum > 0 {
t := tNum % 10
tNum = tNum / 10
rNum = rNum*10 + t
}
if rNum == num {
return true
}
return false
}
func main() {
fmt.Println(bIsPalindromic(101))
iMax := 0
for i := 100; i < 1000; i++ {
for j := 100; j < 1000; j++ {
iPalindromic := i * j
if bIsPalindromic(iPalindromic) {
if iPalindromic > iMax {
iMax = iPalindromic
}
}
}
}
fmt.Println(iMax)
}
https://projecteuler.net/problem=3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
6857
package main
import "fmt"
func main() {
//13195 5 7 13 29
//600851475143
iMaxFactor := 0
num := 600851475143
i := 2
for {
if i > num {
break
}
if num%i == 0 {
num = num / i
iMaxFactor = i
continue
} else {
i++
}
}
fmt.Println(iMaxFactor)
}
https://projecteuler.net/problem=2
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
4613732
package main
import "fmt"
func main() {
a := 1
b := 2
c := 0
sum := 2
for a < 4000000 {
c = a + b
a = b
b = c
if c%2 == 0 {
sum += c
}
}
fmt.Println(sum)
}
https://projecteuler.net/problem=1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
233168
package main
import "fmt"
func main() {
sum := 0
for i := 1; i < 10; i++ {
if i%3 == 0 || i%5 == 0 {
sum += i
}
}
fmt.Println(sum)
}
package main
import (
"fmt"
"math/rand"
"time"
)
func funcname(a interface{}) string {
return a.(string)
}
func main() {
var v interface{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 10; i++ {
v = i
if (r.Intn(100) % 2) == 0 {
v = "hello"
}
if _, ok := v.(int); ok {
fmt.Printf("int %d\n", v)
}
}
}
package main
import "fmt"
type Element interface{}
func main() {
var e Element = 100
switch value := e.(type) {
case int:
fmt.Println("int", value)
case string:
fmt.Println("string", value)
default:
fmt.Println("unknown", value)
}
}
type 是go的一个关键字
package main
import "fmt"
type Stringer interface {
String() string
}
type aa int
func (a aa) String() string {
return "String() function"
}
func test(a interface{}) {
if _, ok := a.(Stringer); ok {
fmt.Println("aa is type of Stringer")
} else {
fmt.Println("aa is not type of Stringer")
}
}
func main() {
var a aa
test(a)
}
Type switches are a form of conversion: they take an interface and, for each case in the switch, in a sense convert it to the type of that case. Here’s a simplified version of how the code under fmt.Printf turns a value into a string using a type switch. If it’s already a string, we want the actual string value held by the interface, while if it has a String method we want the result of calling the method.
type Stringer interface {
String() string
}
var value interface{} // Value provided by caller.
switch str := value.(type) {
case string:
return str
case Stringer:
return str.String()
}
The first case finds a concrete value; the second converts the interface into another interface. It’s perfectly fine to mix types this way.
What if there’s only one type we care about? If we know the value holds a string and we just want to extract it? A one-case type switch would do, but so would a type assertion. A type assertion takes an interface value and extracts from it a value of the specified explicit type. The syntax borrows from the clause opening a type switch, but with an explicit type rather than the type keyword:
value.(typeName)
and the result is a new value with the static type typeName. That type must either be the concrete type held by the interface, or a second interface type that the value can be converted to. To extract the string we know is in the value, we could write:
str := value.(string)
But if it turns out that the value does not contain a string, the program will crash with a run-time error. To guard against that, use the “comma, ok” idiom to test, safely, whether the value is a string:
str, ok := value.(string)
if ok {
fmt.Printf("string value is: %q\n", str)
} else {
fmt.Printf("value is not a string\n")
}
If the type assertion fails, str will still exist and be of type string, but it will have the zero value, an empty string.
As an illustration of the capability, here’s an if-else statement that’s equivalent to the type switch that opened this section.
if str, ok := value.(string); ok {
return str
} else if str, ok := value.(Stringer); ok {
return str.String()
}
package main
import (
"flag"
"fmt"
)
var name = flag.String("name", "gerry", "input username")
var age = flag.Int("age", 27, "input user age")
var ip int
func init() {
flag.IntVar(&ip, "flagname", 1234, "help message for flagname")
}
func main() {
flag.Parse()
fmt.Printf("args = %s, num = %d\n", flag.Args(), flag.NArg())
for i := 0; i != flag.NArg(); i++ {
fmt.Printf("arg[%d] = %s\n", i, flag.Arg(i))
}
fmt.Println(*name)
fmt.Println(*age)
fmt.Println(ip)
}
e:\Go\GOPATH\src\jian\flagDemo>go run flagDemo.go aaa
args = [aaa], num = 1
arg[0] = aaa
gerry
27
1234
e:\Go\GOPATH\src\jian\flagDemo>go run flagDemo.go --name "jian" -age=18 -flagname=33 aaa bbb ccc ddd
args = [aaa bbb ccc ddd], num = 4
arg[0] = aaa
arg[1] = bbb
arg[2] = ccc
arg[3] = ddd
jian
18
33